• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

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

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
try this....



milan.
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Merrill !!
 
Nina Anderson
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ThAnks guys!! I'm able to now upload the image files, create & delete files

I really appreciate your assistance!
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic