上传期间无法接收服务器上的文件内容
我有一个 JSP 页面,其中有一个文件输入字段,我可以从中浏览 csv 文件,然后将其上传到服务器上。我在存在此文件输入字段的表单中使用 method = "POST" 和 ENCTYPE='multipart/form-data' 。
在 servlet 端(在应用程序的 servlet 中),我使用 apache 的通用文件上传 API-ServletFileUpload API。从该 API 的方法 parseRequest(request) 获取 FileItem 列表后,我无法使用 FileItem API 的 getName()、getString() 方法获取文件名及其内容。
需要知道我做错了什么或对我的方法进行任何修改以使我的应用程序正常工作。任何与此相关的指示都会有所帮助。
提前致谢!!
还在应用程序主 servlet 的 doPost 方法中尝试了以下代码:-
`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));
}`
打印的输出是:
DEBUG:---------AttribName = weblogic.servlet.network_channel.port
DEBUG:---------AttribValue=9703
不知道请求参数是否应该仅返回此属性或其他属性。
还尝试了以下代码:
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());
}
}
I have 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());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您确定正在调用您的
doProcess
方法吗?您是否在该代码块之前或其中的其他位置放置了任何System.out
消息?另外,您如何声明文件上传表单?您需要将表单上的
enctype
设置为multipart/form-data
:根据 Apache 文档
也许更容易阅读:
尝试一下,看看它是否有效。
Are you sure your
doProcess
method is being called? Have you placed anySystem.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 tomultipart/form-data
:It looks like your code is mostly correct based on the Apache documentation
A little easier to read maybe:
Give that a shot and see if it works.