输出正常但是在文件夹里没有显示图片

输出正常但是在文件夹里没有显示图片

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

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕阿莹 2020-01-16 16:15:32

同学你好,图片是发布到Tomcat中的,同学可以去Tomcat的webapp(或者wtpwebapp)文件夹下,找到对应的项目名,看一下对应的文件夹里是否有上传的文件,

不是上传到eclipse里项目的对应文件夹中哦。

http://img1.sycdn.imooc.com//climg/5e201b7d09a383cf06200502.jpg

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!


  • 找了一下午,终于看见了,在tomcat文件夹里。(泪奔)
    2020-02-12 16:27:50
  • 终于找到它,还好你没放弃~~~~
    2020-02-12 17:56:21
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师