3

我正在尝试将一个 1kb 的文本文件上传到服务器。无论出于何种原因,我收到的数据都不包含文件信息。所以 FileItem.Write(file) 不起作用。由于 FileItem 说大小为 0。

我认为这不是连接问题,因为我设法收到了 URLVariables。唯一没有通过的是实际文件。

我遵循了 adobe flex 指南,但它仍然不起作用。(http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_7.html

不会引发任何错误,并且会触发事件完成。

有什么建议么?

flex 3.2 sdk Jboss 服务器 java doPost

编辑:添加源代码

CertificateUploadServlet.java

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.ListIterator;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

public class CertificateUploadServlet extends HttpServlet {

public void init(ServletConfig config) throws ServletException {
    super.init(config);
}

public void doPost(HttpServletRequest request, HttpServletResponse response) 
{
    File disk = null;
    FileItem item = null;
    DiskFileItemFactory factory = new DiskFileItemFactory();
    ListIterator iterator = null;
    List items = null;

    Servlet FileUpload upload = new ServletFileUpload(factory);

    try
    {
        items = upload.parseRequest(request);
    }catch (FileUploadException e1)
    {
        // Oh Noes!
    }


    iterator = items.listIterator();

    while(iterator.hasNext())
    {
        item = (FileItem) iterator.next();

        if(item.isFormField())
        {

        }else
        {
            try
            {
                PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("C:/Test/processUploadedFile2.txt",true)));

                out1.println("item.getContentType():\t\t "+item.getContentType());
                out1.println("item.getName:\t\t "+ item.getName());
                out1.println("item.getSize:\t\t" + item.getSize());
                out1.println("item.getString:\t\t" + item.getString());
                out1.println("item.getFieldName:\t\t"+item.getFieldName());
                out1.println("item.isInMemory:\t\t" + item.isInMemory());
                out1.println("item.toString():\t\t" + item.toString());

                out1.close();
            }
            catch(IOException e)
            {
                // oh Noes~
            }
        }
    }
}

}

数据测试.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            layout="horizontal"
            creationComplete="init()">

    <mx:Script>
        <![CDATA[
            import flash.netFileReference;
            import mx.controls.Alert;

            private var fileRef:FileReference = new FileReference();

            private function init():void
            {
                fileRef.addEventListener(Event.SELECT, selectHandler);
                fileRef.addEventListener(Event.COMPLETE, completeHandler);
            }

            private function selectHandler(event:Event):void
            {
                Alert.show("Selected...");

                var request:URLRequest= new URLRequest("https://localhost/scm/uploadServlet");
                fileRef.upload(request);
            }

            private function completeHandler(event:Event):void
            {
                Alert.show("File got uploaded");
            }
            ]]>
    </mx:Script>

    <mx:Button id="mBrowseButton" label="browse..." click="fileRef.browse()" />
</mx:Application>

item.getName:返回我要上传的正确文件名

但 item.getSize 始终返回 0,因此当我尝试在服务器上写入文件时,它始终为空。

更多信息:

我能够将变量添加到 URLVariables 类并在 java 类中检索它。唯一的问题是文件没有被传输。

输出:

Single file upload test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.toString():        name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000000.tmp, size=10bytes, isFormField=true, FieldName=Filename
item name: Filename value: Cookie.txt
item.toString():        name=Cookie.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000001.tmp, size=0bytes, isFormField=false, FieldName=Filedata
item.getName() :         Cookie.txt
item.getContentType():       application/octet-stream
item.getSize:        0
item.getString:      
item.getFieldName:       Filedata
item.isInMemory:         true
item.toString():         name=Cookie.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000001.tmp, size=0bytes, isFormField=false, FieldName=Filedata
item name: Upload   value: Submit Query

Multifile upload test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.toString():        name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000003.tmp, size=10bytes, isFormField=true, FieldName=Filename
item name: Filename value: Cookie.txt
item.toString():        name=Cookie.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000004.tmp, size=0bytes, isFormField=false, FieldName=Filedata

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.toString():        name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000006.tmp, size=10bytes, isFormField=true, FieldName=Filename
item name: Filename value: doPost.txt
item.toString():        name=doPost.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000007.tmp, size=0bytes, isFormField=false, FieldName=Filedata


Single file upload test
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.toString():        name=Cookie.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000010.tmp, size=0bytes, isFormField=false, FieldName=Filedata
item name: Filename value: Cookie.txt
item.getName() :         Cookie.txt
item.getContentType():       application/octet-stream
item.getSize:        0
item.getString:      
item.getFieldName:       Filedata
item.isInMemory:         true
item.toString():         name=Cookie.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\work\jboss.web\localhost\scm\upload_76556a96_14064a51ded__7ffa_00000010.tmp, size=0bytes, isFormField=false, FieldName=Filedata
item name: Upload   value: Submit Query

编辑:更多信息。以下输出来自用户 cs 代码。我无法弄清楚为什么当我按照记录的方式进行操作时它仍然不允许我显示信息。

accept:text/*
content-type:multipart/form-data; boundary=----------ae0ae0ae0GI3ae0GI3ae0Ij5gL6cH2
user-agent:Shockwave Flash
host:localhost
content-length:1019
connection:Keep-Alive
cache-control:no-cache
cookie:JSESSIONID=BE2BF803041A7324CAF21445F6F3389C

------------ae0ae0ae0GI3ae0GI3ae0Ij5gL6cH2
Content-Disposition: form-data; name="Filename"

Cookie.txt
------------ae0ae0ae0GI3ae0GI3ae0Ij5gL6cH2
Content-Disposition: form-data; name="Filedata"; filename="Cookie.txt"
Content-Type: application/octet-stream

en.wikipedia.org    FALSE   /   FALSE   0   BCSI-CS-1b3dbb382aea0366    2
en.wikipedia.org    FALSE   /   FALSE   1404323604  centralnotice_bannercount_fr12  1
en.wikipedia.org    FALSE   /   FALSE   1374590485  centralnotice_bucket    0-4.2
en.wikipedia.org    FALSE   /   FALSE   1373131359  mediaWiki.user.bucket%3Aext.articleFeedbackv5%4011-tracking 11%3Aignore
en.wikipedia.org    FALSE   /   FALSE   1373131359  mediaWiki.user.bucket%3Aext.articleFeedbackv5%405-links 5%3AX
en.wikipedia.org    FALSE   /   FALSE   1373131359  mediaWiki.user.bucket%3Aext.articleFeedbackv5%406-form  6%3A6
en.wikipedia.org    FALSE   /   FALSE   0   uls-previous-languages  %5B%22en%22%5D
------------ae0ae0ae0GI3ae0GI3ae0Ij5gL6cH2
Content-Disposition: form-data; name="Upload"

Submit Query
------------ae0ae0ae0GI3ae0GI3ae0Ij5gL6cH2--

// dumpRequest = false output

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   10
item.getString: Cookie.txt
item.getFieldName:  Filename
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload__272408b1_140738428eb__7ffa_00000006.tmp, size=10bytes, isFormField=true, FieldName=Filename
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   application/octet-stream
item.getName:   Cookie.txt
item.getSize:   0
item.getString: 
item.getFieldName:  Filedata
item.isFormField:   false
item.isInMemory:    true
item.toString():    name=Cookie.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload__272408b1_140738428eb__7ffa_00000007.tmp, size=0bytes, isFormField=false, FieldName=Filedata
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   12
item.getString: Submit Query
item.getFieldName:  Upload
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload__272408b1_140738428eb__7ffa_00000008.tmp, size=12bytes, isFormField=true, FieldName=Upload

更多信息:

版本信息

Apache Maven: 2.2.1
Java version: 1.6.0_39
fileupload: 1.2
Commons-IO: 1.4
Internet Explorer: 8.0.7601.17514
Flash Player: 11.8.800.94

更多输出信息。第一次尝试是使用 cookie.txt 而第二次是 test.txt

#########################True/True
------------GI3gL6Ef1GI3cH2KM7ei4cH2gL6ei4
Content-Disposition: form-data; name="Filename"

Cookie.txt
------------GI3gL6Ef1GI3cH2KM7ei4cH2gL6ei4
Content-Disposition: form-data; name="Filedata"; filename="Cookie.txt"
Content-Type: application/octet-stream

en.wikipedia.org    FALSE   /   FALSE   0   BCSI-CS-1b3dbb382aea0366    2
en.wikipedia.org    FALSE   /   FALSE   1404323604  centralnotice_bannercount_fr12  1
en.wikipedia.org    FALSE   /   FALSE   1374590485  centralnotice_bucket    0-4.2
en.wikipedia.org    FALSE   /   FALSE   1373131359  mediaWiki.user.bucket%3Aext.articleFeedbackv5%4011-tracking 11%3Aignore
en.wikipedia.org    FALSE   /   FALSE   1373131359  mediaWiki.user.bucket%3Aext.articleFeedbackv5%405-links 5%3AX
en.wikipedia.org    FALSE   /   FALSE   1373131359  mediaWiki.user.bucket%3Aext.articleFeedbackv5%406-form  6%3A6
en.wikipedia.org    FALSE   /   FALSE   0   uls-previous-languages  %5B%22en%22%5D
------------GI3gL6Ef1GI3cH2KM7ei4cH2gL6ei4
Content-Disposition: form-data; name="Upload"

Submit Query
------------GI3gL6Ef1GI3cH2KM7ei4cH2gL6ei4--
#########################True/True
------------gL6GI3Ij5Ef1ei4Ef1ae0ei4Ef1gL6
Content-Disposition: form-data; name="Filename"

Test.txt
------------gL6GI3Ij5Ef1ei4Ef1ae0ei4Ef1gL6
Content-Disposition: form-data; name="Filedata"; filename="Test.txt"
Content-Type: application/octet-stream

This is text found instead Test.txt
------------gL6GI3Ij5Ef1ei4Ef1ae0ei4Ef1gL6
Content-Disposition: form-data; name="Upload"

Submit Query
------------gL6GI3Ij5Ef1ei4Ef1ae0ei4Ef1gL6--

#########################False/True
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   10
item.getString: Cookie.txt
item.getFieldName:  Filename
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload__32478800_14078614409__7ffa_00000000.tmp, size=10bytes, isFormField=true, FieldName=Filename
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   application/octet-stream
item.getName:   Cookie.txt
item.getSize:   0
item.getString: 
item.getFieldName:  Filedata
item.isFormField:   false
item.isInMemory:    true
item.toString():    name=Cookie.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload__32478800_14078614409__7ffa_00000001.tmp, size=0bytes, isFormField=false, FieldName=Filedata
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   12
item.getString: Submit Query
item.getFieldName:  Upload
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload__32478800_14078614409__7ffa_00000002.tmp, size=12bytes, isFormField=true, FieldName=Upload
#########################False/True
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   8
item.getString: Test.txt
item.getFieldName:  Filename
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_6fd3dea8_140789cbb78__7ffa_00000003.tmp, size=8bytes, isFormField=true, FieldName=Filename
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   application/octet-stream
item.getName:   Test.txt
item.getSize:   0
item.getString: 
item.getFieldName:  Filedata
item.isFormField:   false
item.isInMemory:    true
item.toString():    name=Test.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_6fd3dea8_140789cbb78__7ffa_00000004.tmp, size=0bytes, isFormField=false, FieldName=Filedata
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   12
item.getString: Submit Query
item.getFieldName:  Upload
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_6fd3dea8_140789cbb78__7ffa_00000005.tmp, size=12bytes, isFormField=true, FieldName=Upload


#########################True/False
[accept:text/*
content-type:multipart/form-data; boundary=----------Ij5KM7ae0Ef1GI3ei4ei4gL6GI3ei4
user-agent:Shockwave Flash
host:localhost
content-length:1019
connection:Keep-Alive
cache-control:no-cache
cookie:JSESSIONID=C8FF29BF4253B2E9B9EEF3360F83EB74
]
#########################True/False
[accept:text/*
content-type:multipart/form-data; boundary=----------GI3cH2ei4KM7ei4GI3GI3KM7gL6ae0
user-agent:Shockwave Flash
host:localhost
content-length:449
connection:Keep-Alive
cache-control:no-cache
cookie:JSESSIONID=B4D506EF25DA8FD0D5B11DBA98B2B21D
]

#########################False/False
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   10
item.getString: Cookie.txt
item.getFieldName:  Filename
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_50024124_140787caa99__7ffa_00000000.tmp, size=10bytes, isFormField=true, FieldName=Filename
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   application/octet-stream
item.getName:   Cookie.txt
item.getSize:   0
item.getString: 
item.getFieldName:  Filedata
item.isFormField:   false
item.isInMemory:    true
item.toString():    name=Cookie.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_50024124_140787caa99__7ffa_00000001.tmp, size=0bytes, isFormField=false, FieldName=Filedata
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   12
item.getString: Submit Query
item.getFieldName:  Upload
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_50024124_140787caa99__7ffa_00000002.tmp, size=12bytes, isFormField=true, FieldName=Upload
#########################False/False
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   8
item.getString: Test.txt
item.getFieldName:  Filename
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_c1b3312_14078bd7c03__7ffa_00000000.tmp, size=8bytes, isFormField=true, FieldName=Filename
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   application/octet-stream
item.getName:   Test.txt
item.getSize:   0
item.getString: 
item.getFieldName:  Filedata
item.isFormField:   false
item.isInMemory:    true
item.toString():    name=Test.txt, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_c1b3312_14078bd7c03__7ffa_00000001.tmp, size=0bytes, isFormField=false, FieldName=Filedata
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
item.getContentType():   null
item.getName:   null
item.getSize:   12
item.getString: Submit Query
item.getFieldName:  Upload
item.isFormField:   true
item.isInMemory:    true
item.toString():    name=null, StoreLocation=C:\test\jboss-eap-5.1\jboss-as\server\test\tmp\upload_c1b3312_14078bd7c03__7ffa_00000002.tmp, size=12bytes, isFormField=true, FieldName=Upload
4

2 回答 2

1

首先,请检查您上传的文件是否是您认为的文件,并且它们没有空内容。

其次,请使用下面的 servlet 代码并针对httphttps协议运行您的示例。您可以将变量更改dumpRequestfalse以打印FileItem与您所拥有的相似的信息。

public class UploadServlet extends HttpServlet {

    private boolean dumpRequest = true;
    private boolean saveToFile = true;
    private String fileName = "requestBody.txt";

    @Override
    public void init() {
        log("Upload servlet initialized");
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        try {

            log("Servicing new request...");


            if (dumpRequest) {

                dumpRequestBody(request, response);

            } else {
                Writer writer = new OutputStreamWriter( response.getOutputStream() );

                FileItem item = null;
                DiskFileItemFactory factory = new DiskFileItemFactory();

                ServletFileUpload upload = new ServletFileUpload(factory);

                List<?>items = upload.parseRequest(request);

                ListIterator<?> iterator = items.listIterator();


                while (iterator.hasNext()) {
                    item = (FileItem) iterator.next();

                    writer.write("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

                    writer.write("item.getContentType():\t " + item.getContentType() + "\n");
                    writer.write("item.getName:\t" + item.getName() + "\n");
                    writer.write("item.getSize:\t" + item.getSize() + "\n");
                    writer.write("item.getString:\t" + item.getString() + "\n");
                    writer.write("item.getFieldName:\t" + item.getFieldName() + "\n");
                    writer.write("item.isFormField:\t" + item.isFormField() + "\n");
                    writer.write("item.isInMemory:\t" + item.isInMemory() + "\n");
                    writer.write("item.toString():\t" + item.toString() + "\n");
                    writer.flush();

                }
                writer.close();
            }

        } catch(Exception e) {
            throw new RuntimeException(e);
        }
    }


    private void dumpRequestBody(HttpServletRequest request, HttpServletResponse response) throws IOException {
        OutputStream responseOut = response.getOutputStream();
        InputStream in = request.getInputStream();

        if (!saveToFile) {
            dumpHeaders(request, responseOut);
            dumpStream(in, responseOut);
        } else {
            File file = new File(fileName);
            FileOutputStream out = new FileOutputStream(file);

            dumpStream(in, out);
            out.close();

            Writer writer = new OutputStreamWriter(responseOut);
            writer.write("Response body was saved to file: " + file.getAbsolutePath());
            writer.close();
        }
        in.close();
        responseOut.flush();
        responseOut.close();
    }

    private void dumpHeaders(HttpServletRequest request, OutputStream out) throws IOException {
        Writer writer = new OutputStreamWriter(out);
        writer.write("[");

        // first dump headers
        Enumeration<String> allHeaders = request.getHeaderNames();
        while (allHeaders.hasMoreElements()) {
            String header = allHeaders.nextElement();
            String value = request.getHeader(header);
            writer.write(header + ":" + value + "\n");
        }
        writer.write("]");
        writer.flush();
    }

    private void dumpStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[2 * 1024];
        int bytesRead = 0;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    }
}

上面的 servlet 在运行时http和一个名为的文件test.txt,其中包含Test data通过简单的 html 页面上传的简单文本行,会产生下面的转储。将您获得的输出(也请用这些输出更新您的问题)与下面的输出进行比较,以便能够进一步解决此问题。

[host:localhost:8080
user-agent:Mozilla/5.0 (Windows NT 6.0; rv:22.0) Gecko/20100101 Firefox/22.0
accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
accept-language:el-gr,el;q=0.8,en-us;q=0.5,en;q=0.3
accept-encoding:gzip, deflate
referer:http://localhost:8080/upload/upload.html
connection:keep-alive
content-type:multipart/form-data; boundary=---------------------------5118710224663
content-length:298
]-----------------------------5118710224663
Content-Disposition: form-data; name="myfile"; filename="test.txt"
Content-Type: text/plain

Test data
-----------------------------5118710224663
Content-Disposition: form-data; name="submit"

Submit
-----------------------------5118710224663--

更新 1:更新了 Servlet 代码和输出。新的 servlet 可以选择将请求正文写入文件。请使用 , 运行两次dumpRequest = truesaveToFile = false以查看响应的结果并更新问题中的输出,并true为两者使用一次,以将请求正文保存在文件中。请将该文件上传到某处。

从 UploadServlet 的第一个输出中可以看出,文件数据存在于请求正文中,但公共文件上传未正确解析它们。如果您使用的是最新版本(即commons- fileupload:1.3,commons-io:2.4),或者如果您使用的是旧版本,您遇到了该错误或它的副作用。

因为最可能的原因是Flash使用的行改变,或者它写入边界的方式有问题。无论如何,如果您可以上传一个带有如上所述生成的响应正文的文件,并且还请提及您的版本:commons-fileupload、commons-io、Flash 播放器、您用于您的场景的浏览器。


更新 2:不幸的是,我无法使用您提供的文件中的请求正文重现您的情况。虽然 Flash 似乎没有创建严格有效的请求(它没有\r\n在文件末尾添加额外的内容),但我可以使用 commons-file-upload 1.2 和 1.3 正常上传这个请求,所以我已经没有想法了。

我建议的最后一件事是尝试使用下面的简单 html 页面从浏览器上传文件。将此文件放在 Web 应用程序的根目录中并上传文件Cookie.txt(确保action表单中的 指向上传 servlet)。如果这不起作用,那么问题出在 commons-file-upload 方面,尽管我无法想象它可能是什么。密切关注服务器日志(也许某个异常被浅化了?)也不会受到伤害。

唯一可以揭示您的问题的安全赌注是下载 commons-file-upload 的源代码,将其放入您的项目中,而不是将其用作依赖项和调试。由于您的情况每次都会出现问题,因此找到它应该不会太难。

上传html:

<html>
<body>

<form action="/scm/uploadServlet" method="post" enctype="multipart/form-data">

  <label for="file">Filename:</label><br/>
  <input type="file" name="myfile" id="file"><br/>
  <input type="submit" name="submit" value="Submit">
</form>

</body>
</html>
于 2013-08-12T11:02:39.177 回答
0

正在读取/上传文件的问题是 fileupload-1.2 没有正确读取数据。Fileupload-1.3 能够正确读取上传的格式。所以 flex 和 fileupload-1.2 之间存在一些错误

于 2013-08-20T23:45:13.823 回答