国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

Apache Commons fileUpload實現(xiàn)文件上傳之一

2019-11-26 14:30:48
字體:
供稿:網(wǎng)友

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

需要兩個jar包:

commons-fileupload.jar

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

利用Servlet來實現(xiàn)文件上傳。

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); // 設(shè)置緩沖區(qū)大小,這里是4kbfactory.setRepository(tempPathFile);// 設(shè)置緩沖區(qū)目錄// Create a new file upload handlerServletFileUpload upload = new ServletFileUpload(factory);// Set overall request size constraintupload.setSizeMax(4194304); // 設(shè)置最大文件尺寸,這里是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) {// 可以跳轉(zhuǎn)出錯頁面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"對應(yīng)web.xml中<servlet-mapping>中<url-pattern>的設(shè)置. --><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> 

這樣就能簡單實現(xiàn)一個文件上傳功能了,當然這是最最最基本的,繼續(xù)研究中。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄骅市| 泰和县| 永靖县| 孝感市| 鱼台县| 永丰县| 东丰县| 罗平县| 波密县| 黎城县| 乐陵市| 鄄城县| 伊春市| 奇台县| 厦门市| 天镇县| 咸宁市| 罗城| 宜阳县| 同德县| 集安市| 岑溪市| 中超| 定州市| 崇阳县| 栾川县| 桂林市| 永平县| 姚安县| 松滋市| 乐安县| 黔东| 罗山县| 门头沟区| 耿马| 鸡东县| 鸡东县| 房产| 岗巴县| 玛多县| 郸城县|