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 

 

 

 

 

 

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