控制台没有输出,上传的文件也不在路径中
package com.imooc.servlet;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 1. 创建磁盘文件项工厂
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
// 2. 创建核心解析类
ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
// 3. 解析请求对象,将请求分成几个部分(FileItem)
try {
List<FileItem> list = fileUpload.parseRequest(request);
// 4. 遍历集合,获取每个部分的对象
for(FileItem fileItem : list) {
// 5. 判断是普通项还是文件上传项
if(fileItem.isFormField()) {
// 普通项
// 获取普通项的名称:
String name = fileItem.getFieldName();
// 获取普通项的值:
String value = fileItem.getString("UTF-8");
System.out.println(name + ":" + value);
}else {
// 文件上传项
// 获取文件名称:
String fileName = fileItem.getName();
// 获取文件的输入流:
InputStream is = fileItem.getInputStream();
// 需要将文件写入到服务器的某个路径:
String path = getServletContext().getRealPath("/upload");
System.out.println(path);
// 创建输出流与输入流进行对接:
OutputStream os = new FileOutputStream(path + "\\" + fileName);
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b)) != -1) {
os.write(b, 0, len);
}
is.close();
os.close();
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星