2011-07-19 13 views
-1

サーブレットをアップロードするには、次のコードを使用しています。しかし、それは繰り返しエラーを投げます。..javaサーブレットファイルのアップロードエラー

import java.io.IOException; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

import java.io.*; 
import java.util.*; 

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 UploadServlet extends HttpServlet { 

    private boolean isMultipart; 
    private String filePath; 
    private int maxFileSize = 50 * 1024; 
    private int maxMemSize = 4 * 1024; 
    private File file ; 

    public void init(){ 
     // Get the file location where it would be stored. 
     filePath = 
      getServletContext().getInitParameter("file-upload"); 
    } 
    public void doPost(HttpServletRequest request, 
       HttpServletResponse response) 
       throws ServletException, java.io.IOException { 
     // Check that we have a file upload request 
     isMultipart = ServletFileUpload.isMultipartContent(request); 
     response.setContentType("text/html"); 
     java.io.PrintWriter out = response.getWriter(); 
     if(!isMultipart){ 
     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet upload</title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     out.println("<p>No file uploaded</p>"); 
     out.println("</body>"); 
     out.println("</html>"); 
     return; 
     } 
     DiskFileItemFactory factory = new DiskFileItemFactory(); 
     // maximum size that will be stored in memory 
     factory.setSizeThreshold(maxMemSize); 
     // Location to save data that is larger than maxMemSize. 
     factory.setRepository(new File("D:\\tmp")); 

     // Create a new file upload handler 
     ServletFileUpload upload = new ServletFileUpload(factory); 
     // maximum file size to be uploaded. 
     upload.setSizeMax(maxFileSize); 
     System.out.println("hello "); 
     try{ 
     // Parse the request to get file items. 
      System.out.println("hello "); 
     List<FileItem> fileItems = upload.parseRequest(request); 

     // Process the uploaded file items 
     Iterator i = fileItems.iterator(); 

     out.println("<html>"); 
     out.println("<head>"); 
     out.println("<title>Servlet upload</title>"); 
     out.println("</head>"); 
     out.println("<body>"); 
     while (i.hasNext()) 
     { 
     FileItem fi = (FileItem)i.next(); 
     if (!fi.isFormField()) 
     { 
      // Get the uploaded file parameters 
      String fieldName = fi.getFieldName(); 
      String fileName = fi.getName(); 
      String contentType = fi.getContentType(); 
      boolean isInMemory = fi.isInMemory(); 
      long sizeInBytes = fi.getSize(); 
      // Write the file 
      if(fileName.lastIndexOf("\\") >= 0){ 
       file = new File(filePath + 
       fileName.substring(fileName.lastIndexOf("\\"))) ; 
      }else{ 
       file = new File(filePath + 
       fileName.substring(fileName.lastIndexOf("\\")+1)) ; 
      } 
      fi.write(file) ; 
      out.println("Uploaded Filename: " + fileName + "<br>"); 
     } 
     } 
     out.println("</body>"); 
     out.println("</html>"); 
    }catch(Exception ex) { 
     System.out.println("brilliant "); 
    } 
    } 

} 

は、しかし、私は取得していますエラーは、そのもブロック:(

私はスタックトレースを印刷しようとしたがexpceptionするつもりはない

SEVERE: Servlet.service() for servlet UploadServlet threw exception 
javax.servlet.ServletException: Servlet execution threw an exception 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:313) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) 
    at java.lang.Thread.run(Unknown Source) 

..です動作しませんでした..構成のためには、私はそれがうまく印刷されたドットストップの方法の中でその罰金.. i gaceのsysoutステートメントを信じています..私はリストをdelcaredしているポイントまで...

+3

catchブロックで例外を呑み込むことを選択した場合、スタックトレースを出力するのではなく、失敗の元の原因を知ることは絶対にありません。 'System.out.println()'の代わりに、例外ハンドラでスタックトレースを出力することをお勧めします。 –

+0

「例外原因」がログに出力されないことを確かめてください – anfy2002us

答えて

1

サーブレットを呼び出す前にエラーが発生しているようです。おそらく設定上の問題です。

+0

これはTomcatで起こっていることですが、あなたのコードにはまだ当てはまりません。 –

関連する問題