Reputation: 7647
I try to read a file which is uploaded through browsing. For that i am using apache commons fileupload jar. So before run it with the project application i simply do a servlet/jsp POC project which successfully reads the file. Below is the code,
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
boolean isMultipart = FileUpload.isMultipartContent(request);
String content = "";
try {
if (isMultipart) {
FileItemFactory factory = new DefaultFileItemFactory();
FileUploadBase upload = new FileUpload(factory);
String[] arr;
List items;
items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
BufferedInputStream buff = new BufferedInputStream(
item.getInputStream());
byte[] bytes = new byte[buff.available()];
buff.read(bytes, 0, bytes.length);
content = new String(bytes);
arr = content.split(",");
}
}
}
request.getRequestDispatcher("success.jsp").forward(request,
response);
} catch (FileUploadException e) {
}
I have use the jar version 1.1 becuase my project directory has that version of the jar and it use java 1.4. With java 1.5 we suppose to use ServletFileUpload instead of FileUpload . But i guess that does not matter where i can do the work with ealier version of jar.
The problem comes when i use it with the real application which use struts 1.1. All seems fine but i dont return items when parsing the request.
items = upload.parseRequest(request);
items is empty here.
So the difference i can find is that POC project which successfull show result is a JSP/Servlet project where the real application is using Struts.
Is the request going to be differe in JSP/Servlet and Struts project in this case?
In both case i use below in the form.
<form action="SampleWeb" method="post" enctype="multipart/form-data">
<input type="file" name="datafile" size="40">
<input type="submit" value="Submit">
Below is the only addition in the struts application,
<action
path="/ReportsAction"
type="come.test.ReportsAction"
name="ReportsForm"
scope="request"
validate="false"
parameter="dispatch">
<forward name="success" path="/reports/A.jsp"/>
<forward name="updatesuccess" path="/common/B.jsp"/>
</action>
Upvotes: 0
Views: 8375
Reputation: 160251
A request is a request.
However, if you're using Struts 1, actually use Struts 1.
The Form, Java Side
Use an ActionForm
with a FormFile
property:
public class YourForm extends ActionForm {
private FormFile file; // Plus public getter/setter
// Etc.
}
You can also do things like validate the content type:
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
if (!"text/plain".equals(getFile().getContentType())) {
// etc.
The Form, JSP Side
<html:form action="/uploadAction" method="post" enctype="multipart/form-data">
<html:file property="file" />
<!-- Etc... -->
The Action
In the action you should copy your file to its final destination (outside the webapp; use a configuration parameter to define where files end up at). Consider using a utility library to do the copy (or move, maybe, there are several options) instead of doing all the work in the action as shown in some tutorials.
http://www.mkyong.com/struts/struts-file-upload-example/
Don't blindly use the same techniques across environments: this work is handled for you.
Also, avoid using Struts 1 for new projects. It's quite old and much more difficult to use than more modern options like Struts 2 or Spring MVC (which in turn are more difficult to use than things like Rails, Grails, Play, etc.)
Upvotes: 1