jsp實現檔案上傳下載

來源:互聯網
上載者:User

標籤:smart   method   orm   multi   https   count   except   throw   ini   

檔案上傳:

upload.jsp

    <form action="uploadServlet" method="post" enctype="multipart/form-data">        作者:<input type="text" name="author"/><br/>        檔案1:<input type="file" name="file"/><br/>        檔案2:<input type="file" name="file"/><br/>        <input type="submit" value="上傳"/>    </form>

UploadServlet:

import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.File;import com.jspsmart.upload.Files;import com.jspsmart.upload.Request;import com.jspsmart.upload.SmartUpload;public class UploadServlet extends HttpServlet {        public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        this.doPost(request, response);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        String author1 = request.getParameter("author");        System.out.println("author1="+author1);                //建立smartUpload組件對象        SmartUpload smartUpload = new SmartUpload();        //初始化當前對象        smartUpload.initialize(this.getServletConfig(), request, response);                //控制總檔案的上傳大小(單位B)(10MB)        smartUpload.setTotalMaxFileSize(1024*1024*10);        //控制單個檔案的上傳大小(單位B)(1MB)        smartUpload.setMaxFileSize(1024*1024);                try {            //上傳檔案            smartUpload.upload();            //擷取smartUpload組件裡的request對象            Request req = smartUpload.getRequest();            String author = req.getParameter("author");            System.out.println("author="+author);                        //擷取上傳檔案            Files files = smartUpload.getFiles();            //擷取單個檔案            for(int i=0;i<files.getCount();i++){                File file = files.getFile(i);                file.saveAs("d:\\upload\\"+file.getFileName());            }                    } catch (Exception e) {            e.printStackTrace();        }            }}

檔案下載:

download.jsp

<img src="img/wa.png"><a href="downLoadServlet?fileName=wawa.jpg">下載</a>

DownloadServlet:

import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.jspsmart.upload.SmartUpload;import com.jspsmart.upload.SmartUploadException;public class DownLoadServlet extends HttpServlet {        public void doGet(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        this.doPost(request, response);    }    public void doPost(HttpServletRequest request, HttpServletResponse response)            throws ServletException, IOException {        //擷取下載的檔案名稱        String fileName = request.getParameter("fileName");                //建立smartUpload組件對象        SmartUpload smartUpload = new SmartUpload();        //初始化當前對象        smartUpload.initialize(this.getServletConfig(), request, response);        //禁止瀏覽器自動開啟下載的檔案        smartUpload.setContentDisposition(null);                try {            smartUpload.downloadFile("download/"+fileName);        } catch (Exception e) {            e.printStackTrace();        }    }}

 

jsp實現檔案上傳下載

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.