Servlet3.0 上傳檔案執行個體,servlet3.0上傳檔案

來源:互聯網
上載者:User

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>



聯繫我們

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