在获取到前段上传的图片,处理完写入本地路径文件夹upload中时,src/webapp/upload路径下是没有任何图片的=
在获取到前段上传的图片,处理完写入本地路径文件夹upload中时,src/webapp/upload路径下是没有任何图片的,只有在out目录下的upload有照片,这个是哪里是出问题了吗???
相关截图:
相关截图:
相关代码:
package com.roan.bms.controller;
import com.alibaba.fastjson.JSON;
import com.roan.bms.entity.Book;
import com.roan.bms.entity.BookC;
import com.roan.bms.service.BookService;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/**
* 图书新增
*/
public class AddBookServlet extends HttpServlet {
private BookService bookService = new BookService();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
this.doGet(request, response);
}
/**
* 图书新增方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*1.初始化FileUpload组件*/
FileItemFactory fileItemFactory = new DiskFileItemFactory();
/*FileItemFactory用于将前端表单的数据转换为一个个FileItem对象*/
/*ServletFileUpload则是为了FileUpload组件提供Java Web的Http请求解析*/
ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
Book book = new Book();
try {
List<FileItem> formData = servletFileUpload.parseRequest(request);
for (FileItem f : formData) {
/*表单项非文件项*/
if (f.isFormField()) {
System.out.println("普通输入项:" + f.getFieldName() + "-" + f.getString("UTF-8"));
switch (f.getFieldName()) {
case "name":
book.setBname(f.getString("UTF-8"));
break;
case "categoryId":
book.setCategoryId(Integer.parseInt(f.getString("UTF-8")));
break;
case "starLevel":
book.setStarLevel(Integer.parseInt(f.getString("UTF-8")));
break;
case "price":
book.setPrice(Float.parseFloat(f.getString("UTF-8")));
break;
default:
break;
}
} else {
/*将文件保存到服务器目录*/
String path = request.getServletContext().getRealPath("upload");
System.out.println("上传文件目录:" + path);
String fileName = UUID.randomUUID().toString();
/*f.getName()得到原始文件名,截取最后一个点后所有字符串*/
String suffix = f.getName().substring(f.getName().lastIndexOf("."));
System.out.println("截取后的名字:" + suffix);
/*f.write();写入目标文件*/
f.write(new File(path, fileName + suffix));
System.out.println(path + fileName + suffix);
book.setUrl("/upload/" + fileName + suffix);
}
}
bookService.insert(book);//新增
} catch (Exception e) {
e.printStackTrace();
}
/*查询全部的图书-动态查询*/
String name = null;
List<BookC> bookCList = bookService.getBookDynamic(name);
request.setAttribute("bookC_list", bookCList);
request.getRequestDispatcher("/book.ftl").forward(request, response);
}
}
20
收起
正在回答 回答被采纳积分+1
2回答
java工程师2020版
- 参与学习 人
- 提交作业 9393 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星