檔案上傳(二)

來源:互聯網
上載者:User

 

使用apache提供的兩個jar實現上傳,分別是commons-io-1.4.jar和commons-io-1.4.jar

 

兩個頁面upload2.jsp和result2.jsp、UploadServlet

 

upload2.jsp上傳兩個檔案

<form action="/struts2/UploadServlet" method="post" enctype="multipart/form-data">
  
 username:<input type="text" name="username"><br>
 password:<input type="password"  name="password"><br>
 
 file1:<input type="file" name="file1"><br>
 
 file2:<input type="file" name="file2"><br>
 
 
  <input type="submit" name="submit" value=" submit ">
 </form>

 

 

UploadServlet.java

 
package com.test.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

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 {

 @SuppressWarnings("unchecked")
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  
  //執行個體化一個磁碟檔案工廠,用來配置上傳組件ServletFileUpload
  DiskFileItemFactory factory = new DiskFileItemFactory();
  
  //設定檔案上傳路徑
  String path = request.getRealPath("/upload");

  //當上傳檔案大於設定大小時,存放臨時檔案的目錄,web根目錄下upload目錄
  factory.setRepository(new File(path));
  
  //設定上傳檔案時用於臨時存放檔案的記憶體大小,這裡是1M,多於的部分將存在硬碟
  factory.setSizeThreshold(1024 * 1024);
  
  //用上面的工廠執行個體化上傳組件
  ServletFileUpload upload = new ServletFileUpload();
  
  //如果ServletFileUpload執行個體中不設定上面的Factory則報java.lang.NullPointerException: No FileItemFactory has been set.
  upload.setFileItemFactory(factory);
  
  try {
   
   //解析request,處理用戶端傳來的所有表單資料,返回list(包括file和非file類型)
   List<FileItem> list = upload.parseRequest(request);

   for (FileItem item : list) {
    // 判斷用戶端傳來的form表單中 是否是一個file類型
    
    //普通表單類型
    if (item.isFormField()) {
     //得到檔案的名稱
     String name = item.getFieldName();
     
     System.out.println(name);
     
     //得到檔案值
     String value = item.getString("gbk");

     request.setAttribute(name, value);
    }
    //檔案類型
    else {
     String name = item.getFieldName();
     
     //form中,類型為file時,getName()方法才能擷取到檔案名稱,否則為空白
     String value = item.getName();

     // 擷取真本文件名 ,針對不同瀏覽器

     int start = value.lastIndexOf("//");

     String fileName = value.substring(start + 1);
     
     
     //儲存檔案名稱到request中
     request.setAttribute(name, fileName);

     // 第一種方式
     
//     //定義檔案輸出資料流
//     OutputStream os = new FileOutputStream(path);
//     
//     //獲得輸入資料流,
//     InputStream is = item.getInputStream();     
//     
//     //將輸入資料流資料寫入輸出資料流中
//     byte[] buffer = new byte[400];
//
//     int length = 0;
//
//     while ((length = is.read(buffer)) > 0) {
//      //將輸入資料流資料寫入輸出資料流中
//      os.write(buffer, 0, length);
//     }
//
//     os.close();
//     is.close();
     
     //第二種方式
     item.write(new File(path,fileName));
    }
   }
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();

  }
  //轉寄
  request.getRequestDispatcher("upload/result2.jsp").forward(request,
    response);

 }
}

 

result2.jsp

  <body>
 username:${requestScope.username }<br>
 
 password:${requestScope.password }<br>
 
 file1:${requestScope.file1 }<br>
 
 file2:${requestScope.file2 }
  </body>

 

 

此時實現檔案上傳的第二種方式,struts底層的實現方式

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.