aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "file upload in directory" Watch "file upload in directory" New topic
Author

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,
BS
[ June 24, 2004: Message edited by: Boon Subra ]
Jonathan Lister
Greenhorn

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;

System.out.println("Hello");

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");
System.out.println("Hello1");

String filepath = "/Client_UpFiles/";
System.out.println(filepath);

try {

System.out.println("Hello2");
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();

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

//close the stream
stream.close();

}
catch (FileNotFoundException fnfe)
{
System.out.println(fnfe);
System.out.println("fnfe");
return null;

}
catch (IOException ioe)
{
System.out.println(ioe);
System.out.println("ioe");
return null;
}

System.out.println(data);

target = new String("success");
}



struts-config.xml
====================
<controller

maxFileSize="200K"
tempDir="/Client_UpFiles/">
</controller>

Pl help me out.
Thanks,
Boon
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:

"D:/Tomcat/../../Client_UpFiles"+fileName;

It worked though.


I have another issue.

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

Thanks,
Boon
Reginald Law
Greenhorn

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
Greenhorn

Joined: Jul 22, 2004
Posts: 1
Hi,,

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: http://aspose.com/file-tools
 
subject: file upload in directory
 
Similar Threads
Apache commons file upload does not work for me.
how to get directory location
Uploading a directory using JSP/JSF
How to pass more than 1 parameters into the sevlet???
Trinidad file upload