jsp+servlet實現文件上傳與下載功能
本文實例為大家分享了jsp servlet實現文件上傳與下載的具體代碼,供大家參考,具體內容如下
上傳:
需要導入兩個包: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.servletfileupload; /** * 上傳附件 * @author new * */ public class uploadannexservlet extends httpservlet { private static string path = ""; public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request, response); } /* * post處理 * (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 { path = this.getservletcontext().getrealpath("/upload"); try { diskfileitemfactory factory = new diskfileitemfactory(); servletfileupload up = new servletfileupload(factory); list ls = up.parserequest(request); for (fileitem fileitem : ls) { if (fileitem.isformfield()) { string fieldname = fileitem.getfieldname(); //getname()返回的是文件名字 普通域沒有文件 返回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);//非常方便的方法 } request.setattribute("result", "上傳文件成功!"); } } } catch (exception e) { e.printstacktrace(); request.setattribute("result", "上傳失敗,請查找原因,重新再試!"); } request.getrequestdispatcher("/pages/admin/annex-manager.jsp").forward( request, response); } }
下載(i/o流)無需導包:
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; /** * 下載文件 * @author * */ public class downloadfilesservlet extends httpservlet { /** * */ private static final long serialversionuid = 8594448765428224944l; public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { dopost(request, response); } /* * 處理請求 * (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); // web絕對路徑 string path = request.getsession().getservletcontext().getrealpath("/"); string savepath = path + "upload"; // 設置為下載application/x-download response.setcontenttype("application/x-download"); // 即將下載的文件在服務器上的絕對路徑 string filenamedownload = savepath + "/" + name; // 下載文件時顯示的文件保存名稱 string filenamedisplay = name; // 中文編碼轉換 filenamedisplay = urlencoder.encode(filenamedisplay, "utf-8"); response.addheader("content-disposition", "attachment;filename=" + filenamedisplay); try { java.io.outputstream os = response.getoutputstream(); java.io.fileinputstream fis = new java.io.fileinputstream( filenamedownload); byte[] b = new byte[1024]; int i = 0; while ((i = fis.read(b)) > 0) { os.write(b, 0, i); } fis.close(); os.flush(); os.close(); } catch (exception e) { } } }
相關文章
- jsp+servlet實現文件上傳與下載功能
- EJB3.0部署消息驅動Bean拋javax.naming.NameNotFoundException異常
- 在JSP中使用formatNumber控制要顯示的小數位數方法
- 秒殺系統(tǒng)Web層設計的實現方法
- 將properties文件的配置設置為整個Web應用的全局變量實現方法
- JSP使用過濾器防止Xss漏洞
- 在JSP頁面中動態(tài)生成圖片驗證碼的方法實例
- 詳解JSP 內置對象request常見用法
- 使用IDEA編寫jsp時EL表達式不起作用的問題及解決方法
- jsp實現局部刷新頁面、異步加載頁面的方法
- Jsp中request的3個基礎實踐
- JavaServlet的文件上傳和下載實現方法
- JSP頁面的靜態(tài)包含和動態(tài)包含使用方法