FileUpload实现文件上传(包含多文件)
2023-09-14 08:57:55 时间
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;
@WebServlet("/FileUploadServlet")
public class FileUploadServlet extends HttpServlet {
private String filePath; // 文件存放目录
private String tempPath; // 临时文件目录
// 初始化
public void init() throws ServletException
super.init();
// 从配置文件中获得初始化参数
ServletContext context = getServletContext();
filePath = context.getRealPath("/") + "/hzmlFile";
tempPath = context.getRealPath("/") + "/hzmlFile";
System.out.println("文件存放目录、临时文件目录准备完毕 ...");
private void process(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException{
res.setContentType("text/plain;charset=gbk");
PrintWriter pw = res.getWriter();
try{
DiskFileItemFactory diskFactory = new DiskFileItemFactory();
// threshold 极限、临界值,即硬盘缓存 1M
diskFactory.setSizeThreshold(4 * 1024);
// repository 贮藏室,即临时文件目录
diskFactory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(diskFactory);
// 设置允许上传的最大文件大小 4M
upload.setSizeMax(4 * 1024 * 1024);
// 解析HTTP请求消息头
List fileItems = upload.parseRequest(req);
Iterator iter = fileItems.iterator();
while(iter.hasNext())
FileItem item = (FileItem)iter.next();
if(item.isFormField())
System.out.println("处理表单内容 ...");
processFormField(item, pw);
}else{
System.out.println("处理上传的文件 ...");
processUploadFile(item, pw);
}// end while()
pw.close();
}catch(Exception e){
System.out.println("使用 fileupload 包时发生异常 ...");
e.printStackTrace();
}// end try ... catch ...
public void doPost(HttpServletRequest req, HttpServletResponse res)
throws IOException, ServletException {
process(req, res);
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { process(req, res); // 处理表单内容 private void processFormField(FileItem item, PrintWriter pw) throws Exception String name = item.getFieldName(); String value = item.getString(); pw.println(name + " : " + value + "\r\n"); // 处理上传的文件 private void processUploadFile(FileItem item, PrintWriter pw) throws Exception // 此时的文件名包含了完整的路径,得注意加工一下 String filename = item.getName(); System.out.println("完整的文件名:" + filename); int index = filename.lastIndexOf("\\"); filename = filename.substring(index + 1, filename.length()); long fileSize = item.getSize(); if("".equals(filename) fileSize == 0) System.out.println("文件名为空 ..."); return; pw.println(filePath); File uploadFile = new File(filePath + "/" + filename); item.write(uploadFile); pw.println(filename + " 文件保存完毕 ..."); pw.println("文件大小为 :" + fileSize + "\r\n"); }
使用commons-fileupload时,文件上传与表单参数同时提交时的后台代码处理 把Form表单是的enctype= multipart/form-data 是基于流的,只能获取一次,重复获取将为空,当上传文件的input控件与hidden的input字段混合时,直接用request.getParameter一般是不行的.
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { process(req, res); // 处理表单内容 private void processFormField(FileItem item, PrintWriter pw) throws Exception String name = item.getFieldName(); String value = item.getString(); pw.println(name + " : " + value + "\r\n"); // 处理上传的文件 private void processUploadFile(FileItem item, PrintWriter pw) throws Exception // 此时的文件名包含了完整的路径,得注意加工一下 String filename = item.getName(); System.out.println("完整的文件名:" + filename); int index = filename.lastIndexOf("\\"); filename = filename.substring(index + 1, filename.length()); long fileSize = item.getSize(); if("".equals(filename) fileSize == 0) System.out.println("文件名为空 ..."); return; pw.println(filePath); File uploadFile = new File(filePath + "/" + filename); item.write(uploadFile); pw.println(filename + " 文件保存完毕 ..."); pw.println("文件大小为 :" + fileSize + "\r\n"); }
使用commons-fileupload时,文件上传与表单参数同时提交时的后台代码处理 把Form表单是的enctype= multipart/form-data 是基于流的,只能获取一次,重复获取将为空,当上传文件的input控件与hidden的input字段混合时,直接用request.getParameter一般是不行的.
相关文章
- JQUERY AJAX无刷新异步上传文件
- iOS开发之网络编程--使用NSURLConnection实现文件上传
- JSP实现文件上传——uploadFile.jsp
- laravel单文件、多文件上传的实现方法
- 基于Ruoyi ,做了个多选项卡页面,对应录入基本信息和文件上传,画面能跨选项卡校验并及时提示
- PHP fastcgi模式大文件上传500错误
- java struts2入门学习实例--使用struts2快速实现多个文件上传
- 【问题解决方案】Xshell连接服务器并实现上传和下载文件
- java安全删除一个文件,防止工具恢复数据
- Linux中使用SecureCRT上传、下载文件命令sz与rz用法实例
- Qt系列文章021-HTTP文件上传
- 使用jQuery OCUpload实现excel文件一键上传功能
- java操作minio:创建bucket及上传文件,client version 7.0.2
- 基于uploadify.js实现多文件上传和上传进度条的显示
- SAP CRM Fiori My Opportunity应用文件上传的技术实现
- Atitit 文件上传功能的实现 图片 视频 目录 1. 上传原理1 1.1. http post编码 multipart / form-data1 1.2. 临时文件模式 最简单2 1.3
- Atitit .h5文件上传 v3
- Atitit 文件上传 架构设计 实现机制 解决方案 实践java php c#.net js javascript c++ python
- Atitit..文件上传组件选型and最佳实践总结(3)----断点续传控件的实现
- 不使用任何框架,手写纯 JavaScript 实现上传本地文件到 ABAP 服务器
- ios swift alamofire 上传图片(文件),上传进度
- 【SpringBoot笔记12】SpringBoot框架实现文件上传和文件下载
- Http服务器实现文件上传与下载(二)
- Http服务器实现文件上传与下载(一)
- Http服务器实现文件上传与下载(三)
- 黑马day15 文件上传&apche的工具包
- leaflet 上传WKT文件,在地图上显示图形(示例代码055)
- ASP.NET文件上传和下载
- WAF——针对Web应用发起的攻击,包括但不限于以下攻击类型:SQL注入、XSS跨站、Webshell上传、命令注入、非法HTTP协议请求、非授权文件访问等
- go语言web开发系列之十三:gin框架实现图片文件上传
- Springboot+WebUploader优雅实现超大文件的上传(一)