jsp實現圖片的上傳和下載

將圖片上傳
1.添加使用jar包, jspSmartUpload.jar,並導包;
2.創建jsp頁面,注意必須是“post”方式提交,form標籤中一定要添加enctype=”multipart/form-data”

<body>
    <form action="<%=request.getContextPath()%>/UpServlet" method="post" enctype="multipart/form-data">
        <p><input type="file" name="file1"></p>
        <p><input type="file" name="file2"></p>
        <p><input type="file" name="file3"></p>
        <p><input type="file" name="file4"></p>
        <p><input type="submit" value="上傳"></p>
    </form>
</body>

3.添加servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1)創建SmartUpload對象,
        SmartUpload su=new SmartUpload();
        //以及初始化initialize(getServletConfig(), request, response);
        su.initialize(getServletConfig(), request, response);
        //2)設置上傳限制(文件的大小,類型)
        su.setMaxFileSize(1024*1024*9);
        su.setAllowedFilesList("jpg,png,gif");
        //3)調用SmartUpload對象的 upload()
        try {
            su.upload();
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //4)設置上傳路徑
        String path=this.getServletContext().getRealPath("images");
        File file=new File(path);
        if(!file.exists()) {
            file.mkdir();//創建文件夾
        }
        //5)讀取上傳文件的個數,開始單個文件的主傳 
        int count=su.getFiles().getCount();
        List<String> urls=new ArrayList<>();
        for (int i = 0; i < count; i++) {
            /*5.1)讀取本次上傳的文件
            5.2)判斷文件的大小,如果爲0則越過
            5.3)改文件名
            5.4)調用文件對象的 saveAs()方法
            5.5)數據庫處理*/
        com.jspsmart.upload.File file1=su.getFiles().getFile(i);
            if(file1.getSize()==0) {
                continue;
            }
        String filename=file1.getFileName();
        try {
            file1.saveAs(path+"/"+filename);
            urls.add("images/"+filename);//要添加相對路徑
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        }
        request.setAttribute("urls", urls);
        request.getRequestDispatcher("success.jsp").forward(request, response);
        //6)修改跳轉路徑 結束

        //7)處理異常,完成跳轉
    }

跳轉至success.jsp中顯示

<body>
    <c:forEach items="${urls }" var="url">
        <img alt="" src="${url }"><br>
    </c:forEach>
</body>

關於下載
在WebContent中建個文件夾,裏面存放要下載的內容
這裏寫圖片描述
jsp頁面

<body>
    <a href="<%=request.getContextPath()%>/DownServlet">下載</a>
</body>

servlet

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /*創建SmartUpload對象,初始化
        獲得文件路徑
            su.setContentDisposition(null);
        調用下載方法downloadFile(下載的文件的路徑);*/
        SmartUpload su=new SmartUpload();
        su.initialize(getServletConfig(), request, response);
        String path=this.getServletContext().getRealPath("down/[Java參考文檔]JDK_API_1_6_zh_CN.CHM");
        su.setContentDisposition(null);//禁止在瀏覽器中打開
        try {
            su.downloadFile(path);
        } catch (SmartUploadException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章