3

This is a follow-up to what I was trying to accomplish here https://stackoverflow.com/questions/7313922/uploading-files-ussing-myfaces-tomahawk-jsf-2-0

I've managed to get the image as an UploadedFile Object, but I can't seem to be able to save it to disk. I want to save it locally (in C:\Temp, for example) such that when I run my app, I can upload a file (test.jpg, for example) from my desktop and see it saved on the server (for example, in C:\Temp).

My bean is pretty simple:

import org.apache.myfaces.custom.fileupload.UploadedFile;
public class PatientBB {  

private UploadedFile uploadedFile; 

public UploadedFile getUploadedFile(){
return this.uploadedFile; 
}  
.
public void setUploadedFile(UploadedFile uploadedFile){     
this.uploadedFile = uploadedFile; 
} 
.
public String actionSubmitImage(){
//This is th part I need help with. how do I save it in my C?
}

I greatly Appreciate all the help, thank you!

Community
  • 1
  • 1
Myy
  • 18,107
  • 11
  • 37
  • 57

2 Answers2

6

As far as I can tell, according to the javaDoc, you should be able to do

uploadedFile.getInputStream();

and then push the data from that to a FileOutputStream.

Psuedo:

 InputStream is = uploadedFile.getInputStream();
 byte[] buffer = new byte[uploadedFile.getLength()); //This can be more space-efficient if necessary
 is.read(buffer);
 File f = new File("C:\\tmp\\" + uploadedFile.getFilename());
 f.createNewFile();
 FileOutputStream fos = new FileOutputStream(f);
 fos.write(buffer);

Does that make sense? Is that what you're looking for?

Cody S
  • 4,744
  • 8
  • 33
  • 64
  • Thanks. I got a file in the right temp folder, so so far so good. but the file is just a file, not an image. what am I suposed to do with that is ( InputStream ) because you declared it, but you didn't use it anywhere. – Myy Dec 14 '11 at 19:46
  • Good catch. Fixed it. The file is an image if the UploadFile is an image. As for reading it in (for instance) Windows Explorer, if the file has the right extension, it should open in an image viewing software. This all depends on what uploadedFile.getFilename() returns (does it return a filename with an extension?). You can also use uploadedFile.getContentType to return the mimetype, which you should be able to translate to the appropriate extension if necessary. – Cody S Dec 14 '11 at 20:09
  • yay! that worked! I was missing the extension '.jpeg' in the Name. But something peculiar happened. when I tried to access the file it opened up Windows Photo viewer with the message "Windows Photo Viewer can't open the file because it is being edited by anotehr program." So I tried opening it up with paint, and a message poped up saying "A sharing violation occured while accessing C:\temp\testImage.jpeg" suddenly after2-3 minutes it worked. any ideas? – Myy Dec 14 '11 at 20:40
  • Your program was probably maintaining a lock on the file. Don't forget to close files when you no longer need them. –  Dec 14 '11 at 21:06
  • @user929694 at the end of it all, make sure you do fos.close() and is.close(). Then you should be good. – Cody S Dec 14 '11 at 21:08
  • oh, thats right. my freshman class just came back to me. I'm so dumb. But Thank you so Much Cody. I really appreciate it. HAppy Holidays! – Myy Dec 14 '11 at 21:59
  • @user929694 No sweat man (I forget that stuff pretty regularly too). Happy holidays to you as well :) – Cody S Dec 14 '11 at 22:03
  • @CodyS I have an xhtml and I'm trying to display the image I just saved, and it looks like this: but it doesn't display. It's an xhtml file in the java project I'm woking on, do you know how I have to call the path in order to retrieve the image and display it? – Myy Dec 15 '11 at 02:08
-1

The uploaded file can be copy to the specified location using java FileUtils API.

File theFile = new File("C:\temp\filename");

eg: FileUtils.copyFile(upload, theFile);

Yuck
  • 49,664
  • 13
  • 105
  • 135
sethupathi.t
  • 502
  • 2
  • 6