Jsp + servlet for file upload and download, servlet File Upload
This article provides examples of the Code for uploading and downloading jsp servlet files for your reference. The details are as follows:
Upload:
Need to import two packages: commons-fileupload-1.2.1.jar, commons-io-1.4.jar
Import java. io. file; import java. io. IOException; import java. util. list; import javax. servlet. servletException; 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. disk. diskFileItemFactory; import org. apache. commons. fileupload. servlet. servletFileU Pload;/*** upload attachment * @ author new **/public class upload?servlet extends HttpServlet {private static String path = ""; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);}/** post processing * (non-Javadoc) * @ see javax. servlet. http. httpServlet # doPost (javax. servlet. http. httpServletRequest, javax. servlet. http. htt PServletResponse) */public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {path = this. getServletContext (). getRealPath ("/upload"); try {DiskFileItemFactory factory = new DiskFileItemFactory (); ServletFileUpload up = new ServletFileUpload (factory); List <FileItem> ls = up. parseRequest (request); for (FileItem fileItem: ls) {if (fileItem. IsFormField () {String FieldName = fileItem. getFieldName (); // getName () returns the file Name. Normal domain. No file returns NULL // String Name = fileItem. getName (); String Content = fileItem. getString ("gbk"); request. setAttribute (FieldName, Content);} else {String nm = fileItem. getName (). substring (fileItem. getName (). lastIndexOf ("\") + 1); File mkr = new File (path, nm); if (mkr. createNewFile () {fileItem. write (mkr); // very convenient Method} request. setAttribute ("result", "File Uploaded successfully! ") ;}} Catch (Exception e) {e. printStackTrace (); request. setAttribute (" result "," Upload Failed. Please find the cause and try again! ") ;}Request. getRequestDispatcher ("/pages/admin/annex-manager.jsp "). forward (request, response );}}
Download (I/o Stream) without importing package:
Import java. io. IOException; import java.net. URLEncoder; import javax. servlet. servletException; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse;/*** download file ** @ author **/public class DownloadFilesServlet extends HttpServlet {/*****/private static final long serialVersionUID = 8594441765428224944l; public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost (request, response);}/** process requests * (non-Javadoc) * @ see javax. servlet. http. httpServlet # doPost (javax. servlet. http. httpServletRequest, javax. servlet. http. httpServletResponse) */public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String name = request. getParameter ("fileName"); System. out. print ("dddddddddd:" + name); // absolute web path String path = request. getSession (). getServletContext (). getRealPath ("/"); String savePath = path + "upload"; // set to download application/x-download response. setContentType ("application/x-download"); // the absolute path of the file to be downloaded on the Server String filenamedownload = savePath + "/" + name; // The file storage name String filenamedisplay = name displayed when the file is downloaded; // convert the Chinese encoding to filenamedisplay = URLEncoder. encode (filenamedisplay, "UTF-8"); response. addHeader ("Content-Disposition", "attachment; filename =" + filenamedisplay); try {java. io. outputStream OS = response. getOutputStream (); java. io. fileInputStream FCM = new java. io. fileInputStream (filenamedownload); byte [] B = new byte [1024]; int I = 0; while (I = Fi. read (B)> 0) {OS. write (B, 0, I. close (); OS. flush (); OS. close () ;}catch (Exception e ){}}}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.