Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Struts and the fly likes file upload in directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "file upload in directory" Watch "file upload in directory" New topic

file upload in directory

Boon Subra
Ranch Hand

Joined: Jul 18, 2001
Posts: 69
Hi All,

I am trying to upload file with reference to the struts-upload.war file.

when I specify a directory to upload in the webapps/myapps directory.
The message says,the file has been uploaded to that particular directory.

But I dont see any file there.

Could you please help me out with this problem.

Thanks and regards,
[ June 24, 2004: Message edited by: Boon Subra ]
Jonathan Lister

Joined: Feb 22, 2004
Posts: 12
The struts file upload example only writes a file if you specify a new file name and tick the "re-name file" box.

If you don't give a target filename & check the box, the uploaded file contents are simply "consumed" .. actually read into a byteArray .. look carefully at the Action class code.
Boon Subra
Ranch Hand

Joined: Jul 18, 2001
Posts: 69
hi ,
Thanks for the response. Based on the example,I configured this way. The problem is the out put on the Tomcat consile says the file is uploaded in the path . But the file isnt seen anywhere.

Pl. see the code snippet.
public ActionForward execute(ActionMapping actionMapping,ActionForm actionForm,
HttpServletRequest request,
HttpServletResponse response) throws IOException,ServletException

HttpSession session = request.getSession();

String insucode = "FMS";
String target= null;
String data= null;


UploadFile theFile = (UploadFile) actionForm;

//retrieve the text data
String desc = theFile.getUPF_filedesc();

//retrieve the file representation
FormFile file = theFile.getUPF_fname();

//retrieve the file name
String fileName= file.getFileName();

//retrieve the content type
//String contentType = file.getContentType();

//retrieve the file size
// String size = (file.getFileSize() + " bytes");

String filepath = "/Client_UpFiles/";

try {

//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();

//write the file to the file specified
OutputStream bos = new FileOutputStream(filepath);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead =, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
data = "The file " +file +" has been written to \"" + filepath + "\"";

//close the stream

catch (FileNotFoundException fnfe)
return null;

catch (IOException ioe)
return null;


target = new String("success");



Pl help me out.
Boon Subra
Ranch Hand

Joined: Jul 18, 2001
Posts: 69
I tried to solve. Instead of given the relative path I gave the full path name of the directory:


It worked though.

I have another issue.

How do I download those files from a jsp.
How do i do the mapping.

Reginald Law

Joined: Jan 21, 2004
Posts: 19
You will have to use the <html:file> struts tag with in a <html:form> tag on your JSP page. Your action class will have to use the UserForm class to get the file name and file contents out.

SCJP 1.4
R Kasa

Joined: Jul 22, 2004
Posts: 1

I used the above post for uploading image files. Great post and thanks a lot for the information.
But I do have a question for this.

I'm uploading image files(.bmp,.gif,.jpeg) to a folder but how can I identify what type of file it is. I mean how can I set the content type of the uploaded file to the new file.
I agree. Here's the link:
subject: file upload in directory
It's not a secret anymore!