Servlet3.0 上傳檔案執行個體,servlet3.0上傳檔案
1、相關函數的說明
(1)request.getSchem()->擷取協議頭,如http
(2)request.getHostName->擷取主機名稱
(3)request.getPort()->擷取連接埠號碼
(4)request.getContextPath()->擷取請求的資源路徑,形如http://localhost::8080下面的ServletDemo
(5)part.getHeader(“content-disposition”)->擷取傳輸的頭部資訊
(6)getServletContext().getRealPath->擷取絕對路徑
2、注意問題
上傳檔案夾必須存在,如果不存在則會報FileNotFoundException(花了好長時間)
3、編程---建立web工程-建立如下檔案
上傳介面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Upload File Index</title></head><body><form action="UpFile" method="post" enctype="multipart/form-data"><table><tr><td>Select File:</td><td><input type="file" name="file"></td></tr><tr><td>Description:</td><td><input type="text" name="description"></td></tr><tr><td colspan="2"><input type="submit" value="Submit"> <input type="reset" value="Reset"></td></tr></table></form></body></html>
處理servlet
import java.io.File;import java.io.IOException;import java.io.PrintWriter;import java.util.UUID;import javax.servlet.ServletException;import javax.servlet.annotation.MultipartConfig;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.Part;/** * Servlet implementation class FileUploadServlet */@WebServlet(name="upFile",urlPatterns={"/UpFile"})@MultipartConfig(maxFileSize=500000,maxRequestSize=-1)public class FileUploadServlet extends HttpServlet {private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public FileUploadServlet() { super(); // TODO Auto-generated constructor stub }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoPost(request, response);}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubPart part=request.getPart("file");String header=part.getHeader("content-disposition");String storePath=this.getServletContext().getRealPath("/temp");String suffix=ParseFileName(header);String name=UUID.randomUUID()+suffix;File f=new File(storePath + File.separator+name);if(!f.exists()){f.createNewFile();}part.write(storePath + File.separator+name);String description= request.getParameter("description"); request.setAttribute("f", name);request.setAttribute("des", description);request.getRequestDispatcher("info.jsp").forward(request, response);}private String ParseFileName(String info){String str;str=info.substring(info.lastIndexOf("."),info.length()-1);return str;}}
顯示介面
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%><% String filename=(String)request.getAttribute("f");String path=request.getContextPath();String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Info Page</title></head><body><h3><%=request.getAttribute("des") %></h3><img src="<%=basePath %>temp/<%=filename%>"></body></html>