aspose file tools*
The moose likes Struts and the fly likes html:file - uploading photo files from jsp into a directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "html:file - uploading photo files from jsp into a directory" Watch "html:file - uploading photo files from jsp into a directory" New topic
Author

html:file - uploading photo files from jsp into a directory

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Hi Guys,

I'm fairly new to struts, so I'm really struggling!!

I need to do the following:

1) read photos from my jsp page
2) save the folder into a folder.

Here's the code I have so far, but I'm stuck:

<<<JSP>>>>>

<body>
<html:form action="/FileUpload" type="org.apache.struts.validator.DynaValidatorForm" enctype="multipart/form-data">
<table>
<tr>
<td align="center" colspan="2">
<font size="4">ADD Pictures Here</font>
</tr>
<tr>
<td align="right">
File Name
</td>
<td>
<html:file property="sourceFile"/>
</td>
<td>
<html:submit>Upload File</html:submit>
</td>
</tr>
</table>


<<<struts-config>>>

<form-bean name="FileUploadForm" type="org.apache.struts.validator.DynaValidatorForm">
<!-- Dynamic properties of the FileUpload Form -->
<form-property name="sourceFile" type="org.apache.struts.upload.FormFile" />
</form-bean>

<action path="/FileUpload" type="com.poshWebApp.action.FileUploadProcessAction" name="FileUploadForm" scope="request" validate="false" >
<forward name="success" path="/web/service/uploadsuccess.jsp"/>
</action>

<<<action class>>>

public class FileUploadProcessAction extends Action {

public ActionForward execute(ActionMapping mapping_,
ActionForm form_, HttpServletRequest request_,
HttpServletResponse response_) throws Exception {

String status = null;

System.out.println("#####Action: entering search detail action class...");

DynaValidatorForm form = (DynaValidatorForm) form_;

FormFile sourceFile = (FormFile) form.get("sourceFile");

String contentType = sourceFile.getContentType();
String fileName = sourceFile.getFileName();
int fileSize = sourceFile.getFileSize();
byte[] fileData = sourceFile.getFileData();

///???

1. how can I save into a folder here???

///???
status = "session";

return mapping_.findForward(status);

}

}


PleaSe hELp!!!
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'd be more interested in FormFile's getInputStream() method than the getData() method. Once you have an Input Stream, writing to an output file is just standard Java using the java.io package.

I'd suggest you check out Java Developer's Almanac Site for some example code on how to write to files using the java.io package. Pay particular attention to the "copy One File to Another" example. Even though you're not copying from one file to another, you are copying from an input stream to an output stream.


Merrill
Consultant, Sima Solutions
Milan Jagatiya
Ranch Hand

Joined: Jan 01, 2007
Posts: 164
hi
try this....



milan.


Milan.<br />I can because I think I can...
RoshaniG Gopal
Ranch Hand

Joined: May 15, 2006
Posts: 180
Hi Merrill,
Though i have used in a situation, but the particular situation wont DownloadAction (http://www.coresw.com/tsdocs/tsdev101/devedition/javadocs/com/cst/terrasoar/struts/actions/DownloadAction.html) and UploadAction(http://struts.apache.org/1.x/struts-apps/struts-examples/apidocs/org/apache/struts/webapp/upload/UploadAction.html) be useful without bothering for much of the code?


Regards,<br />Roshani
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Roshani,

DownloadAction is for downloads only. Not very useful for an upload. The second link refers to a class that was created specifically for use in the example application. It's useful as an example, but not for much more than that.

Since there are so many different things you can do with an uploaded file (save to disk, save as BLOB, save as CLOB, turn into a PDF, etc.) there isn't really a single class that is going to cover all the bases. You pretty much have to crank out the class and do the low level work.
RoshaniG Gopal
Ranch Hand

Joined: May 15, 2006
Posts: 180
Thanks Merrill !!
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
ThAnks guys!! I'm able to now upload the image files, create & delete files

I really appreciate your assistance!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: html:file - uploading photo files from jsp into a directory