我的第一个博客:JSP的文件上传(一)-----------JSP学习之路(一)
在实现上传功能之前首先准备好Apache的第三方包来提供方法:
思路如下:
一、前端页面部分:
前端部分并没有什么可注意的,唯一要注意的就是,提交方法一定是post方法,后面注意enctype的属性是multipart/form-data格式
代码如下:
<html>
<head>
<title>$Title$</title>
</head>
<body>
<div>
<form action="Up3Servlet" method="post" enctype="multipart/form-data">
<table>
<tr>
<td>上传用户:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>文件1:</td>
<td><input type="file" name="user_file1"></td>
<td><input type="submit" value="上传"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
二、后台servlet部分:
1.先把request请求改成utf-8格式
request.setCharacterEncoding(“utf-8”);
response.setCharacterEncoding(“utf-8”);
response.setContentType(“text/json;charset=UTF-8”);
2.要准备文件的上传工厂
DiskFileItemFactory();
3.文件上传和核心类
ServletFileUpload(factory);
4.得到部署在tomcat下面的项目中的upload路径
request.getServletContext().getRealPath("/upload");
5.解析request对象
upload.parseRequest(request);
注:此方法返回的是一个list数组,所以后面进行读取和写入操作时候需要用循环
6. 得到上传文件的输入流,得到字符流到缓冲区
getInputStream();
7.对IO流进行拷贝操作:①创建要拷贝到的文件 ②准备输出流 ③进行读写操作
new File(文件保存路径+File.separator+文件名);
注:File.separator是分隔符
8.通过循环刷新字节流写到新建文件中
read();write();
附上源代码
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 javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@WebServlet("/Up3Servlet")
public class Up3Servlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("Up3Servlet");
// //1.enctype="multipart/form-data"是通过getParameter是取不到值的
// String name = request.getParameter("username");
// System.out.println(name);
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=UTF-8");
//1.文件的上传工厂
DiskFileItemFactory factory = new DiskFileItemFactory();
//2.文件上传的和核心类
ServletFileUpload upload = new ServletFileUpload(factory);
//3.得到部署在tomcat下面的项目的项目中的upload路径
String uploadPath = request.getServletContext().getRealPath("/upload");
System.out.println("uploadPath:"+uploadPath);
try {
//3.解析request对象
List<FileItem> fileItems = upload.parseRequest(request);
for (FileItem fileItem: fileItems ) {
String name = fileItem.getName();//文件名称
String fieldName = fileItem.getFieldName();//表单属性名称
System.out.println("name:"+name);
System.out.println("fieldName:"+fieldName);
//4.得到上传文件的输入流,得到字符流到缓冲区
InputStream ips = fileItem.getInputStream();
//5.IO流的拷贝操作:①创建文件②准备输出流③进行读写操作
File file = new File(uploadPath+File.separator+name);//先创建一个文件,文件路径+分隔符+文件名称
System.out.println("file:"+file);
FileOutputStream ops = new FileOutputStream(file);//输出流
byte[] date = new byte[2048];//字节流需要刷新
int read = 0 ;
try {
while ((read = ips.read(date, 0, date.length)) != -1) {//利用循环来读取
//read方法中的值代表读到那个数组中去、从0开始、一次读length个
ops.write(date, 0, read);//写到date数组中去、从0开始、一次读多少写多少
}
}catch (Exception e){
e.printStackTrace();
}finally {
//关闭资源
if(ips!=null){
ips.close();
}
if (ops!=null){
ops.close();
}
}
}
}catch(FileUploadException e){
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
注:如果写完代码后找不到上传的文件,可以打印一次request.getServletContext().getRealPath("/upload");来确定文件的上传地址