实现html完成上传任意⽂件,在html中使⽤inputtype=file表单提
交,并且在。。。
代码:(借鉴菜鸟教程)
HTML:
1 <%@ page language="java" contentType="text/html; charset=UTF-8"
2    pageEncoding="UTF-8"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
4    "/TR/html4/loose.dtd">
5 <html>
6 <head>
7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
8 <title>⽂件上传实例 - 菜鸟教程</title>
9 </head>
10 <body>
11 <h1>⽂件上传实例 - 菜鸟教程</h1>
12 <form method="post" action="/TomcatTest/UploadServlet" enctype="multipart/form-data">
13    选择⼀个⽂件:
14    <input type="file" name="uploadFile" />
15    <br/><br/>
16    <input type="submit" value="上传" />
17 </form>
18 </body>
19 </html>
html中提交表单用什么属性servlet:
package st;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
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.apachemons.fileupload.FileItem;
import org.apachemons.fileupload.disk.DiskFileItemFactory;
import org.apachemons.fileupload.servlet.ServletFileUpload;
/**
* Servlet implementation class UploadServlet
*/
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
// 上传⽂件存储⽬录
private static final String UPLOAD_DIRECTORY = "upload";
// 上传配置
private static final int MEMORY_THRESHOLD  = 1024 * 1024 * 3;  // 3MB
private static final int MAX_FILE_SIZE      = 1024 * 1024 * 40; // 40MB
private static final int MAX_REQUEST_SIZE  = 1024 * 1024 * 50; // 50MB
/**
* 上传数据及保存⽂件
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// 检测是否为多媒体上传
if (!ServletFileUpload.isMultipartContent(request)) {
// 如果不是则停⽌
PrintWriter writer = Writer();
writer.println("Error: 表单必须包含 enctype=multipart/form-data");
writer.flush();
return;
}
// 配置上传参数
DiskFileItemFactory factory = new DiskFileItemFactory();
// 设置内存临界值 - 超过后将产⽣临时⽂件并存储于临时⽬录中
factory.setSizeThreshold(MEMORY_THRESHOLD);
/
/ 设置临时存储⽬录
factory.setRepository(new Property("pdir")));
ServletFileUpload upload = new ServletFileUpload(factory);
// 设置最⼤⽂件上传值
upload.setFileSizeMax(MAX_FILE_SIZE);
// 设置最⼤请求值 (包含⽂件和表单数据)
upload.setSizeMax(MAX_REQUEST_SIZE);
// 中⽂处理
upload.setHeaderEncoding("UTF-8");
// 构造临时路径来存储上传的⽂件
// 这个路径相对当前应⽤的⽬录
String uploadPath = ServletContext().getRealPath("./") + File.separator + UPLOAD_DIRECTORY;        // 如果⽬录不存在则创建
File uploadDir = new File(uploadPath);
if (!ists()) {
uploadDir.mkdir();
}
try {
// 解析请求的内容提取⽂件数据
@SuppressWarnings("unchecked")
List<FileItem> formItems = upload.parseRequest(request);
if (formItems != null && formItems.size() > 0) {
// 迭代表单数据
for (FileItem item : formItems) {
// 处理不在表单中的字段
if (!item.isFormField()) {
String fileName = new Name()).getName();
String filePath = uploadPath + File.separator + fileName;
File storeFile = new File(filePath);
// 在控制台输出⽂件的上传路径
System.out.println(filePath);
// 保存⽂件到硬盘
item.write(storeFile);
request.setAttribute("message",
"⽂件上传成功!");
}
}
}
} catch (Exception ex) {
request.setAttribute("message",
"错误信息: " + ex.getMessage());
}
// 跳转到 message.jsp
request, response);
}
}
获取HTML:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>⽂件上传结果</title>
</head>
<body>
<center>
<h2>${message}</h2>
</center>
</body>
</html>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。