博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Servlet文件上传
阅读量:5123 次
发布时间:2019-06-13

本文共 3560 字,大约阅读时间需要 11 分钟。

1、环境 : JDK 1.6 , Tomcat 7.0

2、第三方类库:

    commons-fileupload-1.3.1.jar

    commons-io-2.4.jar

3、web.xml配置:

FileUpload
java02.rhythmk.com.FileUploadServlet
filepath
uploadfile
temppath
temp
FileUpload
/FileUpload.html

 

   4、 jsp :

     

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>    
Insert title here
rhythmk::
上传文件:

 

  5、Servlet:

 

package java02.rhythmk.com;import java.io.File;import java.io.IOException; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; 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 org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * Servlet implementation class FileUploadServlet */ @WebServlet("/FileUploadServlet") public class FileUploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static String filepath = ""; private static String temp = ""; /** * Default constructor. */ public FileUploadServlet() { // TODO Auto-generated constructor stub } @Override public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub super.init(config); if (filepath == "" || temp == "") { filepath = config.getInitParameter("filepath"); temp = config.getInitParameter("temppath"); ServletContext context = getServletContext(); filepath = context.getRealPath(filepath); temp = context.getRealPath(temp); System.out.println("filepath:" + filepath + ",temp:" + temp); } } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse * response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); try { DiskFileItemFactory foctory = new DiskFileItemFactory(); // 设置临时文件 foctory.setRepository(new File(temp)); // 设置临时文件缓存区大小 foctory.setSizeThreshold(1024 * 1024); ServletFileUpload sFileUpload = new ServletFileUpload(foctory); List
list = (List
) sFileUpload .parseRequest(request); for (FileItem item : list) { String name = item.getFieldName(); if (item.isFormField()) { System.out.println("filedName:" + name + ",value:" + item.getString()); } else { String filename = item.getName(); System.out.println("value:" + filename); if (filename.length() > 0) { System.out.println("文件保存路径:" + filepath + "\\" + filename); File file = new File(filepath); if (!file.exists()) { file.mkdir(); } // 保存文件 item.write(new File(filepath, filename)); response.getWriter().write( "文件保存路径:" + filepath + "\\" + filename); } } } } catch (Exception e) { e.printStackTrace(); } } }

转载于:https://www.cnblogs.com/hoobey/p/5402531.html

你可能感兴趣的文章
使用Xshell密钥认证机制远程登录Linux
查看>>
OpenCV之响应鼠标(三):响应鼠标信息
查看>>
Android 画图之 Matrix(一)
查看>>
List<T>列表通用过滤模块设计
查看>>
【模板】最小生成树
查看>>
设计模式之结构型模式
查看>>
poj2569
查看>>
使用pygal_maps_world.i18n中数据画各大洲地图
查看>>
sql server必知多种日期函数时间格式转换
查看>>
jQuery EasyUI 的下拉选择combobox后台动态赋值
查看>>
timeline时间轴进度“群英荟萃”
查看>>
python if else elif statement
查看>>
网络编程
查看>>
文本隐藏(图片代替文字)
查看>>
java面试题
查看>>
提高码力专题(未完待续)
查看>>
pair的例子
查看>>
前端框架性能对比
查看>>
uva 387 A Puzzling Problem (回溯)
查看>>
12.2日常
查看>>