2011-11-07 3 views
0
내 JSP 측에서이 코드를

에 업로드 된 파일을 얻을 수 있습니다.는 어떻게 JSP 양식

protected void doPost(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     try { 

      Part p1 = request.getPart("file"); 
      InputStream is = p1.getInputStream(); 


      Part p2 = request.getPart("photoname"); 
      Scanner s = new Scanner(p2.getInputStream()); 
      String filename = s.nextLine(); 



      // get filename to use on the server 
      String outputfile = "C:\\Documents and Settings\\Sherman\\Desktop\\ImoveiSP\\ImoveiSP\\DB_Scripts" + filename + ".jpg"; 
      //outputfile = outputfile + ".jpg"; 
      //System.out.println("out = " + outputfile); 
      FileOutputStream os = new FileOutputStream(outputfile); 

      // write bytes taken from uploaded file to target file 
      int ch = is.read(); 
      while (ch != -1) { 
       os.write(ch); 
       ch = is.read(); 
      } 
      os.close(); 
      out.println("<h3>File uploaded successfully! </h3>"); 


      File file = new File("C:\\Documents and Settings\\Sherman\\Desktop\\ImoveiSP\\ImoveiSP\\DB_Scripts" + filename + ".jpg"); 
      uploadAmazon(file, "ibagem", ""); 

     } catch (Exception ex) { 
      out.println("Exception -->" + ex.getMessage()); 
     } finally { 
      out.close(); 
     } 
    } 

이 서블릿은 업로드 된 파일을 받아 디스크에 저장 :

그리고 서블릿에서

, 나는 코드가 있습니다. 나는이 코드에 대한 두 가지 질문이

: JSP 측에서

  1. 을 어떻게 난 단지 .JPG 또는 .MPG 파일을 전송하는 사용자를 강제 할 수 있습니까?
  2. jsp 쪽에서 업로드 할 입력을 하나 이상 넣으면 서블릿에서 모든 것을받는 방법은 무엇입니까?
+0

가능한 [JSP/Servlet의 파일 업로드 방법] (http://stackoverflow.com/questions/2422468/how-to-upload-files-in-jsp-servlet/2424824#2424824) – BalusC

답변

1

이 모든 것을 처리하려면 Commons FileUpload을 사용하는 것이 좋습니다. 그것은 일을 훨씬 쉽게 만든다. See the user guide for details.

여러 파일을 처리하는 것은 단일 파일을 처리하는 것과 같습니다.

절대 경로 이름을 구성 매개 변수 btw로 옮기는 것이 좋습니다.