2013-08-06 4 views
3

서버에 1kb의 단일 텍스트 파일을 업로드하려고합니다. 어떤 이유로 든 수신 한 데이터에는 파일 정보가 포함되어 있지 않습니다. 따라서 FileItem.Write (file)가 작동하지 않습니다. FileItem 이후 크기가 0이라고합니다.플렉스에 업로드 된 파일이 없습니다.

URLVariables를 관리했기 때문에 이것이 연결 문제라고 생각하지 않습니다. 실제로 나오지 않는 유일한 것은 실제 파일입니다.

어도비 플렉스 가이드를 따라 갔지만 여전히 작동하지 않습니다. (http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_7.html)

오류가 발생하지 않으며 이벤트 완료가 트리거됩니다.

제안 사항?

플렉스 3.2 SDK JBoss 서버 자바의 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~ 
      } 
     } 
    } 
} 

}

dataTest.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 클래스에 변수를 추가하고 자바 클래스를 검색 할 수 있어요

. 유일한 문제는 파일이 전송되지 않는다는 것입니다.

출력 :

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 

편집 : 더 많은 정보를 원하시면. 다음 출력은 사용자 c.s 코드에서 가져온 것입니다. 나는 문서화 된 방식대로 정보를 보여주지 못하는 이유를 알 수 없습니다.

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 

더 많은 정보를 출력. 두 번째는 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 
+1

FileItem이란 무엇입니까? 서버 또는 클라이언트의 무언가입니까? "URLVariables"는 무엇과 관련이 있습니까? 세트는 어디에 있으며 어떻게 액세스하고 있습니까? 클라이언트에서 에스더로 파일을 어떻게 업로드하려고합니까?어디서 오류를 찾으십니까? 코드를 공유 할 수 있습니까? – JeffryHouser

+0

코드가 없으므로 TV가 없어서 Homer가 미치지 않습니다. b/c 그는 자신의 질문에 답할 수 없으며 자신이하는 일을 보여줄 수 있습니다. –

+0

더 명확하게하기위한 코드가 추가되었습니다. – user1301708

답변

0

파일의 문제는 읽기/업로드되는이 파일 업로드-1.2는 데이터를 올바르게 읽지 않았다고했다. Fileupload-1.3은 업로드 된 형식을 올바르게 읽을 수있었습니다. 그래서 플렉스와 파일 업로드 사이에 몇 가지 버그가 있습니다. -

1

첫째 동안 첫 번째 시도는 업로드 된 파일은 당신이 그들이 생각하는 사람 있는지 확인하시기 바랍니다 cookie.txt을 사용하고 그들은 빈 내용이없는 것이다.

두 번째로 아래 서블릿 코드를 사용하고 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(); 
    } 
} 

위의 서블릿 http에 출마 아래 간단한 HTML 페이지를 통해 덤프를 생성 업로드 텍스트 Test data의 간단한 라인을 포함 test.txt라는 이름의 파일. 이 문제를 해결하기 위해 얻은 결과를 비교하십시오 (귀하의 질문을 해당 결과로 업데이트하십시오).

[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 : 서블릿 코드 및 출력이 업데이트되었습니다. 새로운 서블릿에는 요청 본문을 파일에 기록 할 수있는 옵션이 있습니다. dumpRequest = true, saveToFile = false을 두 번 실행하여 응답 결과를 확인한 다음 질문에 대한 출력을 업데이트하고 두 번 모두 true을 사용하여 요청 본문을 파일에 저장하십시오. 해당 파일을 어딘가에 업로드하십시오.

UploadServlet의 첫 번째 출력에서 ​​볼 수 있듯이 파일 데이터는 요청 본문에 있지만 commons file upload에 의해 올바르게 구문 분석되지 않습니다. 최신 버전 (예 : commons-fileupload : 1.3, commons-io : 2.4)을 사용하고 있거나 이전 버전을 사용하고있는 경우 버그가 발생합니다 (commons upload and Flash don't play nice together은 처음이 아닐 것입니다). 그 버그 또는 그것의 부작용.

가능한 가장 큰 원인은 Flash에서 사용하는 행 변경 사항이나 경계를 작성하는 방식에 문제가 있기 때문입니다. 어쨌든 위와 같이 생성 된 응답 본문을 가진 파일을 업로드 할 수 있으면 좋을 것입니다. 또한 commons-fileupload, commons-io, Flash player, 시나리오 용 브라우저에 대한 버전을 알려주십시오. 업데이트 2


: 불행히도 나는 당신이 제공 한 파일에서 요청의 몸을 사용하여 상황을 재현 할 수 없습니다. 플래시가 엄격하게 유효한 요청을 작성하지 않는 것 같지만 (파일의 끝에 \r\n을 추가하지 않은 것 같습니다) 나는이 요청을 commons-file-upload 1.2와 1.3 모두와 함께 정상적으로 업로드 할 수 있으므로 아이디어가 부족합니다 .

내가 제안하는 마지막 것은 아래의 간단한 html 페이지를 사용하여 브라우저에서 파일을 업로드하는 것입니다. 이 파일을 웹 응용 프로그램의 루트에두고 파일 Cookie.txt을 업로드하십시오 (양식의 action이 업로드 서블릿을 가리키는 지 확인하십시오). 이것이 작동하지 않는다면 나는 그것이 무엇인지 상상할 수는 없지만 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> 
+0

원본 게시물에 출력 데이터를 추가했습니다. 코드가 작동하지만 else 문에 데이터가 들어 있지 않은 이유를 모르겠습니다. – user1301708

+0

@ user1301708 어떤 버전의 commons-file-upload를 사용하십니까? –

+0

@ user1301708 내 대답의 마지막에 내 업데이트를 참조하십시오 –

관련 문제