Apache Commons fileUpload實現檔案上傳之一_java

來源:互聯網
上載者:User

廢話不多說了,直奔主題了。

需要兩個jar包:

commons-fileupload.jar

Commons IO的jar包(本文使用commons-io-2.4.jar)

利用Servlet來實現檔案上傳。

package web.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.File;import java.util.Iterator;import java.util.List;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;/*** Servlet implementation class UploadServlet*/@WebServlet("/UploadServlet")public class UploadServlet extends HttpServlet {private static final long serialVersionUID = 1L;private String uploadPath = "D:\\temp"; // 上傳檔案的目錄private String tempPath = "d:\\temp\\buffer\\"; // 臨時檔案目錄File tempPathFile;public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {try {// Create a factory for disk-based file itemsDiskFileItemFactory factory = new DiskFileItemFactory();// Set factory constraintsfactory.setSizeThreshold(4096); // 設定緩衝區大小,這裡是4kbfactory.setRepository(tempPathFile);// 設定緩衝區目錄// Create a new file upload handlerServletFileUpload upload = new ServletFileUpload(factory);// Set overall request size constraintupload.setSizeMax(4194304); // 設定最大檔案尺寸,這裡是4MBList<FileItem> items = upload.parseRequest(request);// 得到所有的檔案Iterator<FileItem> i = items.iterator();while (i.hasNext()) {FileItem fi = (FileItem) i.next();String fileName = fi.getName();if (fileName != null) {File fullFile = new File(fi.getName());File savedFile = new File(uploadPath, fullFile.getName());fi.write(savedFile);}}System.out.print("upload succeed");} catch (Exception e) {// 可以跳轉出錯頁面e.printStackTrace();}}public void init() throws ServletException {File uploadFile = new File(uploadPath);if (!uploadFile.exists()) {uploadFile.mkdirs();}File tempPathFile = new File(tempPath);if (!tempPathFile.exists()) {tempPathFile.mkdirs();}}}

jsp

<%@ page language="java" contentType="text/html; charset=ISO--"pageEncoding="utf-"%><!DOCTYPE html PUBLIC "-//WC//DTD HTML . Transitional//EN" "http://www.w.org/TR/html/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=GB"><title>File upload</title></head><body><!-- // action="fileupload"對應web.xml中<servlet-mapping>中<url-pattern>的設定. --><form name="myform" action="UploadServlet" method="post"enctype="multipart/form-data">File:<br><input type="file" name="myfile"><br><br><input type="submit" name="submit" value="Commit"></form></body></html> 

這樣就能簡單實現一個檔案上傳功能了,當然這是最最最基本的,繼續研究中。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.