javaWEB文件上传
程序员文章站
2022-05-08 17:02:25
...
Servlet
package top.biglin;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
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.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;
public class FileuploadServlet2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
/*
* (non-Javadoc)
*
* @see
* javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest
* , javax.servlet.http.HttpServletResponse)
*/
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
// 接收上传的文件
// 1、创建磁盘文件项工厂
String path2 = this.getServletContext().getRealPath("temp");
DiskFileItemFactory factory = new DiskFileItemFactory(1024 * 1024,
new File(path2));
// 2、创建文件上传的核心类
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");// 解决文件名称乱码
// 判断是否为文件上传表单
boolean isMutilpartContent = ServletFileUpload.isMultipartContent(request);
if (isMutilpartContent) {
// 分解表单
List<FileItem> parseRequest = null;
try {
parseRequest = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
if (parseRequest != null) {
for (FileItem item : parseRequest) {
boolean isFormField = item.isFormField();
if (isFormField) {
String fieldName = item.getFieldName();
String value = item.getString("UTF-8");
System.out.println(fieldName + ":" + value);
} else {// 表示为文件项
String fileName = item.getName();
InputStream in = item.getInputStream();
String path = this.getServletContext().getRealPath("upload");
OutputStream out = new FileOutputStream(path+"/"+fileName);
//IOUtils ioUtils = new IOUtils();
IOUtils.copy(in, out);
in.close();
out.close();
//删除临时文件
item.delete();
}
}
}
} else {
// 使用request.getParameter()获取参数
}
// 5、判断普通项
doGet(request, response);
}
}
jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>文件上传</title>
</head>
<body>
<div>
<form enctype="multipart/form-data" action="/testfileupload/upload2" method="post">
<p>用户名:<input type="text" name="username" value="" /></p>
<input type="file" name="fileupload" value="dddd" />
<p><input type="submit" name="fileUpload"
value="a上传" /></p>
</form>
</div>
</body>
</html>
下一篇: JavaWEB SSH文件上传
推荐阅读
-
qq音乐怎么上传歌词到网络?qq音乐歌词上传方法图文介绍
-
Android操作Excel文件的功能实现
-
windows media player翻录音乐文件放在哪里如何更改其位置
-
下载压缩文件winrar竟然提示中毒该怎么办?
-
filezilla二进制上传功能介绍,filezilla上传文件操作方法
-
filezilla无法启动文件传输错误的原因是什么?
-
es文件浏览器出现错误,es文件浏览器无法找到服务器怎么解决?
-
filezilla上传后文件不见了是什么原因? filezilla设置隐藏文件的方法介绍
-
filezilla如何上传网页网站,filezilla上传网页网站的方法
-
IBM SPSS Modeler 18 Premium 中文永久破解版安装教程(破解文件)