Unable to receive file contents on server during upload
I h开发者_JS百科ave a JSP page in which I have a file input field from which I browse a csv file and then upload it on server. I am using method = "POST" and ENCTYPE='multipart/form-data' in the form in which this file input field is present.
On the servlet side(in the application's servlet) I am making use of apache's common file upload API-ServletFileUpload API. After getting the FileItem list from the method parseRequest(request) of this API I am unable to get the file name and its content by using the methods getName(), getString() of FileItem API.
Needed to know what am I doing wrong or any modifications in my approach that will make my application to work. Any pointers regarding this will be helpful.
Thanks in advance!!
Also tried the following code in the doPost method of application's main servlet:-
`Enumeration enumAttrib = request.getAttributeNames();
while(enumAttrib.hasMoreElements()) {
String attribName = (String)(enumAttrib.nextElement());
System.out.println("DEBUG:---------AttribName = " + attribName);
System.out.println("DEBUG:---------AttribValue=" + request.getAttribute(attribName));
}`
The output that got printed was:
DEBUG:---------AttribName = weblogic.servlet.network_channel.port
DEBUG:---------AttribValue=9703
Don't know whether the request parameter should return only this or other attributes also.
Also tried following code:
if (ServletFileUpload.isMultipartContent(request)) {
System.out.println("Inside ApplicationMainServlet request is multipart ");
// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);
try
{
// Parse the request
List /* FileItem */items = upload.parseReques(request);
// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext())
{
FileItem item = (FileItem) iter.next();
String name = item.getFieldName();
System.out.println("ApplicationMainServlet name: "+item.getFieldName() + ", val: "+item.getString() );
if (!item.isFormField())
{
//Item is a file
try{
InputStream is = item.getInputStream();
InputStreamReader ir = new InputStreamReader(is);
BufferedReader br = new BufferedReader(ir);
System.out.println("br : " + br);
String fileContent = "";
String strLine;
while((strLine = br.readLine()) != null){
System.out.println("strLine : " + strLine);
if(fileContent != null)
{
fileContent = fileContent+ strLine + "\n";
}
else
{
fileContent = strLine + "\n";
}
}
System.out.println("fileContent : " + fileContent);
} catch(Exception e){
e.printStackTrace();
}
System.out.println("ApplicationMainServlet file name " + item.getName()+",size "+item.getSize());
}
}
Are you sure your doProcess
method is being called? Have you placed any System.out
messages elsewhere before this block of code or in it?
Also how are you declaring your form for the file upload? You need to set the enctype
on the form to multipart/form-data
:
<form enctype="multipart/form-data" name="uploadForm" action="uploadFormAction">
</form>
It looks like your code is mostly correct based on the Apache documentation
if (!item.isFormField()) {
System.out.println("ApplicationMainServlet: name = " + item.getFieldName() + ", val = " + item.getString() + ", file size = " + item.getSize());
}
A little easier to read maybe:
if (!item.isFormField()) {
String fieldName = item.getFieldName();
String fileName = item.getName();
long sizeInBytes = item.getSize();
System.out.println("ApplicationMainServlet: name = " + fieldName + ", val = " + fileName + ", file size = " + sizeInBytes);
}
Give that a shot and see if it works.
精彩评论