老师,图片没有被保存到upload文件中
package com.imooc.web.servlet; 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.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; import com.imooc.mainclass.Book; import com.imooc.utils.UploadUtils; @WebServlet("/addBookServlet") public class addBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; public addBookServlet() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String,String> map=new HashMap<String,String>(); //创建磁盘文件项工厂 DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory(); //创建核心解析类 ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory); //解析请求对象,将请求分成几个部分FileItem try { List<FileItem> list=fileUpload.parseRequest(request); //遍历集合,获得各个部分的对象 for(FileItem fileItem:list) { if(fileItem.isFormField()) { String name=fileItem.getFieldName(); String value=fileItem.getString("UTF-8"); map.put(name, value); System.out.println(map); }else { String fileName=fileItem.getName(); String uuidFileName=UploadUtils.getUuidFileName(fileName); InputStream is=fileItem.getInputStream(); String path=getServletContext().getRealPath("/upload"); String url= path+"\\"+uuidFileName; map.put("path", request.getContextPath()+"/upload/"+uuidFileName); OutputStream os=new FileOutputStream(url); int len=0; byte[] b=new byte[1024]; while((len=is.read(b))!=-1) { os.write(b, 0, len); } is.close(); os.close(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Book book=new Book(); book.setBookId(map.get("bookId")); book.setBookName(map.get("bookName")); book.setPrice(Float.parseFloat(map.get("bookPrice"))); book.setDescription(map.get("remarks")); book.setPath(map.get("path")); response.sendRedirect(request.getContextPath()+"/bookList.jsp"); } }
显示错误的地方是这里
OutputStream os=new FileOutputStream(url);
8
收起
正在回答 回答被采纳积分+1
4回答
Mr__Gao
2019-09-02 17:03:39
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>图书后台管理</title> <link rel="stylesheet" href="css/index.css"> <link rel="stylesheet" href="css/bootstrap.min.css"> </head> <body> <header> <div class="container"> <nav> <a href="${pageContext.request.contextPath}/bookList.jsp" >图书信息管理</a> </nav> <nav> <a href="${pageContext.request.contextPath}/categoryList.jsp" >分类管理</a> </nav> </div> </header> <section class="banner"> <div class="container"> <div> <h1>图书管理系统</h1> <p>图书信息管理</p> </div> </div> </section> <section class="main"> <div class="container"> <form class="form-horizontal" action="/searchBook" method="post"> <div class="form-group" style="float: right;"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-primary">查询</button> </div> </div> <div class="form-group" style="float: right;width: 300px;"> <div class="col-sm-8"> <input name="searchContent" class="form-control" id="searchContent" placeholder="输入要查询的分类" style="width: 250px"> </div> </div> </form> </div> <div class="container"> <table class="table table-striped"> <thead> <tr> <th>序号</th> <th>图书编号</th> <th>图书名称</th> <th>分类</th> <th>价格</th> <th>图书封面</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach items="${sessionScope.bookList }" var="bookList" varStatus="idx"> <c:if test="${sessionScope.bookList!=null}"></c:if> <tr id="${idx.index+1}"> <td>${idx.index+1}</td> <td>${bookList.bookId}</td> <td>${bookList.bookName}</td> <td>${bookList.categoryBook}</td> <td>¥${bookList.price}</td> <td><img src="${bookList.path}"></td> <td> <a href="/updateBook?bookId=book0001">修改</a> <a href="/deleteBook?bookId=book0001">删除</a> </td> <!--在循环显示数据时,此处的book0001可以用EL表达式进行替换--> </tr> </c:forEach> </tbody> </table> </div> </section> <section class="page"> <div class="container"> <div id="fatie"> <a href="${pageContext.request.contextPath}/addBook.jsp"><button>新建</button></a> </div> </div> </section> <footer> copy@慕课网 </footer> </body> </html>
老师,我在bookList。jsp中用<c:foreach>显示上传后的图片出现一个500的错误。
我在控制台能够得到图片的地址,但是在显示的时候就这样了
Mr__Gao
2019-09-02 13:01:11
package com.imooc.web.servlet; 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.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; import com.imooc.mainclass.Book; import com.imooc.utils.UploadUtils; @WebServlet("/addBookServlet") public class addBookServlet extends HttpServlet { private static final long serialVersionUID = 1L; public addBookServlet() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Map<String,String> map=new HashMap<String,String>(); //创建磁盘文件项工厂 DiskFileItemFactory diskFileItemFactory=new DiskFileItemFactory(); //创建核心解析类 ServletFileUpload fileUpload=new ServletFileUpload(diskFileItemFactory); //解析请求对象,将请求分成几个部分FileItem try { List<FileItem> list=fileUpload.parseRequest(request); //遍历集合,获得各个部分的对象 for(FileItem fileItem:list) { if(fileItem.isFormField()) { String name=fileItem.getFieldName(); String value=fileItem.getString("UTF-8"); map.put(name, value); System.out.println(map); }else { String fileName=fileItem.getName(); System.out.println(fileName); String uuidFileName=UploadUtils.getUuidFileName(fileName); System.out.println(uuidFileName); InputStream is=fileItem.getInputStream(); String path=getServletContext().getRealPath("/upload"); System.out.println(path); String url= path+"\\"+uuidFileName; System.out.println(url); map.put("path", request.getContextPath()+"/upload/"+uuidFileName); OutputStream os=new FileOutputStream(url); int len=0; byte[] b=new byte[1024]; while((len=is.read(b))!=-1) { os.write(b, 0, len); } is.close(); os.close(); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } Book book=new Book(); book.setBookId(map.get("bookId")); book.setBookName(map.get("bookName")); book.setPrice(Float.parseFloat(map.get("bookPrice"))); book.setDescription(map.get("remarks")); book.setPath(map.get("path")); response.sendRedirect(request.getContextPath()+"/bookList.jsp"); } }
package com.imooc.utils; import java.util.UUID; public class UploadUtils { public static String getUuidFileName(String fileName) { int idx=fileName.lastIndexOf("."); String exName=fileName.substring(idx); String UUidFileName=UUID.randomUUID().toString().replace("-", "")+exName; return UUidFileName; } }
控制台输出的信息都有,就是没保存到upload文件夹中
芝芝兰兰
2019-09-02 11:45:52
同学你好。首先要确定自己的项目中有没有upload文件夹,可以将filename、path、uuidFileName都使用System.out.println输出一下看看。这些路径都存在吗?是否有错误或者null
另外,目录中如果含有中文,可能会导致错误,可以将中文的路径,经如下转换,如:
path = URIDecoder.decode(path,"utf-8");
如果还是报错,同学可以将自己的UploadUtils类、调试及报错信息,复制粘贴出来。并且将项目的目录结构截个图。注意不要直接贴在回复中,会丧失代码格式。可以以回答这个问题的方式贴入~
祝学习愉快~
2. 从网页搭建入门JavaWeb
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星