Servlet 上传文件,重启tomcat后文件和文件夹会自动删除,需要保存上传文件

当上传文件后会传到服务器,如果再重新部署后,就为空了!

那么就得解决这个问题!

(1)在工程中创建upload目录,暂存放上传的文件;

 (2)  需要在D:\apache-tomcat-8.5.38_java61\conf\server.xml文件添加如下映射路径:

 

(3)在servlet中修改代码:

@WebServlet(name = "UploadsServlet", urlPatterns = "/myup")
@MultipartConfig   //使用MultipartConfig注解标注改servlet能够接受文件上传的请求
public class UploadsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //编码统一
        response.setContentType("text/html;charset=utf-8");
        //写入对象
        PrintWriter out = response.getWriter();

        //1.获得part对象 (<input type="file" name="img"/>)
        Part part = request.getPart("img");

        //2.获得part的头信息
//        Content-Disposition:form-data; name="file"; filename="test.txt"
        String header = part.getHeader("Content-Disposition");
        System.out.println(header);

        //3.截取上传文件的扩展名 .jpg,.txt, docx...
        String stufName = header.substring(header.lastIndexOf("."), header.length() - 1);
        System.out.println(stufName);

        //4.文件名UUID32位随机数
//        String fileName = UUID.randomUUID() + stufName;
//        System.out.println(fileName);

        //获取文件名
        String fileName=header.substring(header.lastIndexOf("\\")+1,header.length()-1);
        System.out.println(fileName);



        //5.文件输入流
        InputStream is = part.getInputStream();
        //6.服务器路径(Tomcat)
        String path = request.getServletContext().getRealPath("/upload");

        //文件创建文件夹upload
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }

        //7.拼接完整的路径
        //C:\Users\44401\OneDrive\桌面\img\timg (1).jpg
        String filePath = file + "/" + fileName;
        System.out.println(filePath);

        //8)写入
        FileOutputStream oos = new FileOutputStream(filePath);
        byte[] bys = new byte[1024];
        int length = 0;
        while ((length = is.read(bys)) != -1) {
            //写入
            oos.write(bys, 0, length);
        }

        //关闭流
        oos.close();
        is.close();


        ////////////////////////////////////////////////
        //将服务器路径拷贝到这个目录下哦!
        String destPath="E:\\jdbcLesson\\myupload0701\\src\\main\\webapp\\upload\\"+fileName;
        /////)写入到另一个文件中
        FileInputStream fis = new FileInputStream(filePath);
        FileOutputStream oos1 = new FileOutputStream(destPath);
        byte[] bys1 = new byte[1024];
        int length1 = 0;
        while ((length1 = fis.read(bys1)) != -1) {
            //写入
            oos1.write(bys1, 0, length1);
        }
        //关闭流
        oos1.close();
        fis.close();
        ///////////////////////////////////////////////////////

        out.println("上传成功");
        //关闭
        out.flush();
        out.close();

    }

(4)在JSP页面效果:

<img src="upload/timg (1).jpg" width="200px" height="200px" >

也可以直接访问: http://localhost:8082/upload/timg (5).jpg 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章