找不到指定路径问题
package com.imooc.util;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
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;
public class UploadUtils {
public static Map<String, Object> UploadFile(String uploadDirectory, HttpServletRequest request)
throws ServletException, IOException {
Map<String, Object> map = new HashMap<String, Object>();
try {
// 创建一个磁盘文件工厂对象
DiskFileItemFactory factory = new DiskFileItemFactory();
// 创建一个核心解析类
ServletFileUpload upload = new ServletFileUpload(factory);
// 解析request请求,返回的是list集合,list集合中存放的是FileItem对象
List<FileItem> list= upload.parseRequest(request);
String url=null ;
// 遍历集合,将每个表单项存储到Map集合
for (FileItem fileItem : list) {
if (fileItem.isFormField()) {
String name=fileItem.getFieldName();
String value=fileItem.getString("utf-8");
System.out.println(name+" "+value);
map.put(name, value);
} else {
String fileName = fileItem.getName();
System.out.println(fileName);
// 获取文件的随机名
String uuidFileName = UuidUtils.getUUIDFileName(fileName);
// 获取文件上传的数据
InputStream is = fileItem.getInputStream();
// 将输入流对接到输出流
url = uploadDirectory + "\\" + uuidFileName;
OutputStream os = new FileOutputStream(url);
int len = 0;
byte[] bytes = new byte[1024];
while ((len = is.read(bytes)) != -1) {
os.write(bytes, 0, len);
}
is.close();
os.close();
// 将文件地址保存到集合
}
}
} catch (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
}
package com.imooc.servlet;
import java.io.IOException;
import java.util.Map;
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 com.imooc.model.FoodDaoImpl;
import com.imooc.util.UploadUtils;
/**
* 添加菜品信息的Servlet
*/
@WebServlet("/FoodAddServlet")
public class FoodAddServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//将文件上传路径导入
Map<String,Object> map=UploadUtils.UploadFile("/upload", request);
FoodDaoImpl fdi=new FoodDaoImpl();
fdi.addFood(map);
request.getSession().setAttribute("list", fdi.getFoodByName(request.getParameter("foodName")));
response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
正在回答 回答被采纳积分+1
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星