Servlet3.0 上传文件实例

Servlet3.0 上传文件实例,第1张

概述1、相关函数的说明(1)request.getSchem()-&gt获取协议头,如http(2)request.getHostName-&gt获取主机名(3)request.getPort()-&gt获取端口号(4)request.getContextPath()-&gt获取请求的资源路径,形如http://loc 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->获得@R_502_6128@

2、注意问题

上传文件夹必须存在,如果不存在则会报fileNotFoundException(花了好长时间)

3、编程---新建web工程-新建以下文件

上传界面

<%@ page language="java" ContentType="text/HTML; charset=ISO⑻859⑴" pageEnCoding="ISO⑻859⑴"%><!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⑻859⑴"><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=⑴)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 stub doPost(request,response); } /** * @see httpServlet#doPost(httpServletRequest request,httpServletResponse response) */ protected voID doPost(httpServletRequest request,IOException { // Todo auto-generated method stub Part 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()⑴); return str; }}

显示界面

<%@ page language="java" ContentType="text/HTML; charset=ISO⑻859⑴" pageEnCoding="ISO⑻859⑴"%><% 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⑻859⑴"><Title>Info Page</Title></head><body><h3><%=request.getAttribute("des") %></h3><img src="<%=basePath %>temp/<%=filename%>"></body></HTML>



总结

以上是内存溢出为你收集整理的Servlet3.0 上传文件实例全部内容,希望文章能够帮你解决Servlet3.0 上传文件实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1018864.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存