文件上传 commons-fileupload
来源:互联网 发布:淘宝网运动器材练臂力 编辑:程序博客网 时间:2024/04/29 02:14
首先是上传页面
fileupload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'fileUpload.jsp' starting page</title>
</head>
<body>
<form action="../servlet/FileUpload" method="post" enctype="multipart/form-data">
<br>
文件一:<input type="file" name="file1" /> <br>
文件二:<input type="file" name="file2" /> <br>
上传者: <input type="text" name="uploader"/> <br>
日期: <input type="text" name="date"/> <br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
传如servlet 处理
public class FileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String fileDir = "F:/";
/**
* Constructor of the object.
*/
public FileUpload() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
doPost();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置编码
response.setContentType("text/html");
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//核心代码
try {
DiskFileItemFactory fileFactory = new DiskFileItemFactory(); //产生工厂
ServletFileUpload fu = new ServletFileUpload(fileFactory); //调用工具
fu.setFileSizeMax(30*1024*1024); //设置文件大小限制
List fileItems = fu.parseRequest(request); // 获取请求项
Iterator iter = fileItems.iterator();
String uploader = null;
String date = null;
List<String> fileNames = new ArrayList<String>();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) { // 判断文件
String oldFileName = item.getName();
String fieldName = item.getFieldName();
String content = item.getString();
String type = item.getContentType();
String newFileName = null;
// System.out.println("fileName :" +oldFileName +" and fileName : "+ fieldName + " and content :" + content + " and type "+ type);
int delimiter = oldFileName.lastIndexOf("/");
// System.out.println(delimiter);
if (delimiter == -1) {
newFileName = oldFileName.substring(delimiter + 1);
System.out.println(newFileName);
}else{
newFileName = oldFileName;
}
if(newFileName != null && !newFileName.trim().equals("")){
fileNames.add(newFileName);
item.write(new File(fileDir + "first.txt")); // 设置写入地址,自己设定
}
} else { // 表单
String fieldName = item.getFieldName();
if ("uploader".equals(fieldName)) {
uploader = item.getString();
} else if ("date".equals(fieldName)) {
date = item.getString();
}
}
}
System.out.println("forword");
// request.setAttribute("fileNames",fileNames);
// request.getRequestDispatcher("../admin/fileUploadResult.jsp").forward(request, response); // 后续处理
} catch (Exception e) {
}
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
- 文件上传 commons-FileUpload
- Commons FileUpload上传文件
- Commons FileUpload上传文件
- commons-fileupload-文件上传
- commons fileupload 文件上传
- commons-fileupload上传文件
- 文件上传 commons-fileupload
- commons-fileupload 文件上传问题
- commons fileupload 文件上传应用
- Commons-fileupload 文件的上传
- commons-fileupload文件上传、下载
- 使用commons fileupload 上传文件
- commons-fileupload文件上传、下载
- 文件上传 commons-fileupload.jar
- 使用 Commons-fileupload 上传文件
- commons-fileupload 实现文件上传
- Commons-Fileupload 实现文件上传
- Servlet上传文件,commons-fileupload
- 通用的recyclerview adapter 适配
- 80. Remove Duplicates from Sorted Array II
- PO RELEASE 采购订单审核(支持多级审核)
- 数据结构实验之图论六:村村通公路
- LeetCode - 205. Isomorphic Strings
- 文件上传 commons-fileupload
- Swift中FMDB的使用教程和事务的使用
- Google开源实时通信项目WebRTC
- 73. Set Matrix Zeroes
- BroadcastReceiver探讨之动态广播取消注册流程
- ajax的简单使用
- Http post请求
- 表单提交后台,点击按钮不进后台
- android之视频播放系统VideoView和自定义VideoView控件的应用