输出正常但是在文件夹里没有显示图片
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.service.UserService;
import com.imooc.service.impl.UserServicelmpl;
import com.imooc.utils.UploadUtils;
import com.imooc.web.listener.User;
/**
* Servlet implementation class RegistServlet
*/
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接受数据
Map<String,String> map = new HashMap<String,String>();
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
ServletFileUpload fileUpload = new ServletFileUpload(diskFileItemFactory);
List<FileItem> list;
try {
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);
}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 (FileUploadException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
User user = new User();
user.setUserName(map.get("username"));
user.setPassword(map.get("password"));
user.setPath(map.get("path"));
System.out.println(user);
UserService userService = new UserServicelmpl();
List<User>userList = (List<User>)getServletContext().getAttribute("userList");
userService.regist(userList, user);
System.out.println(userList);
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 676 份
- 解答问题 9666 个
本阶段将从前端网页搭建入手,到Java Web基础,前后端结合助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星