上传期间无法接收服务器上的文件内容

发布于 2024-10-12 04:36:10 字数 3155 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2024-10-19 04:36:10

您确定正在调用您的 doProcess 方法吗?您是否在该代码块之前或其中的其他位置放置了任何 System.out 消息?

另外,您如何声明文件上传表单?您需要将表单上的 enctype 设置为 multipart/form-data

<form enctype="multipart/form-data" name="uploadForm" action="uploadFormAction">
</form>

根据 Apache 文档

if (!item.isFormField()) {
    System.out.println("ApplicationMainServlet: name = " + item.getFieldName() + ", val = " + item.getString() + ", file size = " + item.getSize());
}

也许更容易阅读:

 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);
 }

尝试一下,看看它是否有效。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文