2011-11-07 5 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(); 
     } 
    } 

このサーブレットは、アップロードされたファイルを取得してディスクに保存します。私はこのコードの約2つの質問持つ

: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

複数のファイルを処理することは、1つのファイルを処理することと同じです。

絶対パス名を構成パラメータbtwに移動することをお勧めします。