JSP實(shí)現(xiàn)快速上傳文件的方法
本文實(shí)例講述了JSP實(shí)現(xiàn)快速上傳文件的方法。分享給大家供大家參考。具體如下:
這里演示JSP不使用第三方庫(kù),實(shí)現(xiàn)快速上傳文件的功能
1. FileUpload.java:
package FileUpload; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import javax.servlet.ServletInputStream; /** * */ /** * @author Qch * */ public class FileUpload { ServletInputStream in=null; String fpath="C://"; public FileUpload() { fpath="C://"; in=null; } public void setInputStream(ServletInputStream in) { this.in=in; } public void setFpath(String p) { this.fpath=p; } public String getFpath() { return fpath; } public String getParameter() { String r=null; try { r=getParameter(in); } catch (Exception e) { e.printStackTrace(); } return r; } public long getFileUpload() { long r=-1; try { r=getFileUpload(in,fpath); } catch (Exception e) { e.printStackTrace(); } return r; } public String getParameter(ServletInputStream in)// 只能按順序提取 throws Exception { int l = 0; byte[] b = new byte[1024]; l = in.readLine(b, 0, b.length);// 依次是讀取屬性的開始符、名稱、屬性值的類型、屬性的值 String si = new String(b); if (si.startsWith("----------------------------")) {// 表示是從開始符開始讀,否則應(yīng)為剛讀取文件后的一個(gè)屬性,此時(shí)應(yīng)少讀一次 l = in.readLine(b, 0, b.length); } l = in.readLine(b, 0, b.length); l = in.readLine(b, 0, b.length); String value = new String(b, 0, l); return value; } public long getFileUpload(ServletInputStream in, String fpath)// 需要提供輸入流和存儲(chǔ)路徑 throws Exception { // out.println("文件信息:<br>"); long begin = System.currentTimeMillis();// 傳送時(shí)間計(jì)時(shí)開始 int l = 0; byte[] b = new byte[1024]; l = in.readLine(b, 0, b.length); String sign = new String(b, 0, l);// eg.-----------------------------7d9dd29630a34 l = in.readLine(b, 0, b.length); String info = new String(b, 0, l);// eg.Content-Disposition:form-data; // name="file"; l = in.readLine(b, 0, b.length); // String type=new // String(b,0,l);//eg.Content-Type:application/octet-stream(程序文件) l = in.readLine(b, 0, b.length); // String nulll=new String(b,0,l);//此值應(yīng)為空 int nIndex = info.toLowerCase().indexOf("filename=\""); int nLastIndex = info.toLowerCase().indexOf("\"", nIndex + 10); String filepath = info.substring(nIndex + 10, nLastIndex); int na = filepath.lastIndexOf("\\"); String filename = filepath.substring(na + 1); // out.println("文件絕對(duì)路徑:"+filepath+"<br>"); // out.println("文件名:"+filename+"<br><br>"); String path=fpath + filename; File fi = new File(path);// 建立目標(biāo)文件 if (!fi.exists()&&!fi.createNewFile()) return -2; BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream( fi)); while ((l = in.readLine(b, 0, b.length)) > 0) { if (l == sign.length()) { String sign1 = new String(b, 0, sign.length()); // out.println(sign1+"<br>"); if (sign1.startsWith(sign))// 比對(duì)是否文件已傳完 break; } f.write(b, 0, l); f.flush(); } f.flush(); f.close(); long end = System.currentTimeMillis();// 傳送時(shí)間計(jì)時(shí)結(jié)束 // out.println("上傳文件用時(shí):"+(end-begin)+"毫秒<br>"); return end - begin; } }
2. submitFile.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'submitFile.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script language="javascript"> function check() { if (document.form2.name.value==""){ alert("姓名不能為空??!"); document.form2.name.focus(); return false; } if (document.form2.file.value==""){ alert("文件不能為空!!"); return false; } return true; } </script> </head> <body> <br> <form method="post" name="form2" enctype="MULTIPART/FORM-DATA" action="AnswerFile.jsp"> <br> <p align="center"> <br> </p> <table width="530" border="1" bgcolor="#c0c0c0" align="center" height="91"> <tbody> <tr> <td valign="top" align="right"> 姓名 <br> </td> <td valign="top"> <input type="text" name="name"> </td> </tr> <tr> <td align="right"> 文件 </td> <td align="left"> <input type="file" name="file"> </td> </tr> <tr> <td valign="top" align="right"> 文件類型 <br> </td> <td valign="top" align="left"> <select size="1" name="leixing"> <option selected value="作業(yè)"> 作業(yè) </option> <option value="課程設(shè)計(jì)"> 課程設(shè)計(jì) </option> <option value="論文"> 論文 </option> </select> </td> </tr> <tr> <td align="right"> <input type="Submit" value="上傳" name="button2" onclick="return(check());"> </td> <td align="left"> <input type="reset" value="重置" name="button3"> </td> </tr> </tbody> </table> <p> <br> <br> </p> </form> </body> </html>
3. AnswerFile.jsp:
<%@ page language="java" import="java.util.*,java.io.*" pageEncoding="GB18030"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'AnswerFile.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <jsp:useBean id="upload" scope="session" class="FileUpload.FileUpload"/> <jsp:setProperty name="upload" value="C://" property="fpath"/> <% ServletInputStream in = request.getInputStream(); upload.setInputStream(in); String nam = upload.getParameter(); out.println("姓名:" + nam + "<br><br>"); long time = upload.getFileUpload(); out.println("文件上傳完畢,總共耗時(shí):" + time + "毫秒<br>"); String leixing = upload.getParameter(); out.println("文件類型:" + leixing + "<br>"); in.close(); %> <br> <div align="right"> <a href="index.jsp">回到首頁(yè)</a> </div> </body> </html>
希望本文所述對(duì)大家的JSP程序設(shè)計(jì)有所幫助。
- Jsp頁(yè)面實(shí)現(xiàn)文件上傳下載類代碼
- JSP上傳文件到指定位置實(shí)例代碼
- jsp中點(diǎn)擊圖片彈出文件上傳界面及預(yù)覽功能的實(shí)現(xiàn)
- jsp實(shí)現(xiàn)文件上傳下載的程序示例
- AJAX和JSP實(shí)現(xiàn)的基于WEB的文件上傳的進(jìn)度控制代碼
- jsp文件上傳與下載實(shí)例代碼
- jsp 文件上傳瀏覽,支持ie6,ie7,ie8
- jsp+ajax實(shí)現(xiàn)無刷新上傳文件的方法
- 用JSP編寫文件上傳
- JSP實(shí)用教程之簡(jiǎn)易文件上傳組件的實(shí)現(xiàn)方法(附源碼)
相關(guān)文章
JSP動(dòng)態(tài)實(shí)現(xiàn)web網(wǎng)頁(yè)登陸和注冊(cè)功能
這篇文章主要介紹是動(dòng)態(tài)實(shí)現(xiàn)web網(wǎng)頁(yè)登陸和注冊(cè)功能的示例代碼,文中代碼講解詳細(xì),對(duì)我們學(xué)習(xí)JSP有一定的幫助,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2021-12-12基于jsp實(shí)現(xiàn)新聞管理系統(tǒng) 附完整源碼
這篇文章主要介紹了基于jsp的新聞管理系統(tǒng),具有一定的參考價(jià)值,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09jsp傳值本地?zé)o亂碼測(cè)試機(jī)出現(xiàn)亂碼問題解決
在本地使用jsp傳值時(shí),一切正常無亂碼,當(dāng)放到測(cè)試機(jī)上是發(fā)現(xiàn)有亂碼現(xiàn)象,針對(duì)此問題本文給出詳細(xì)的解決方案,感興趣的朋友可以參考下哈2013-04-04JSP中springmvc配置validator的注意事項(xiàng)
這篇文章主要介紹了JSP中springmvc配置validator的注意事項(xiàng)的相關(guān)資料,并說明springmvc中spring-servlet.xml、applicationContext.xml的區(qū)別需要的朋友可以參考下2017-07-07jsp中實(shí)現(xiàn)上傳圖片即時(shí)顯示效果功能
上傳圖片時(shí)即時(shí)顯示圖片效果,這在項(xiàng)目開發(fā)中時(shí)很常見的一項(xiàng)功能,接下來介紹此功能的實(shí)現(xiàn)過程,有需要的朋友可以參考下2013-01-01jsp利用echarts實(shí)現(xiàn)報(bào)表統(tǒng)計(jì)的實(shí)例
echarts用來做數(shù)據(jù)報(bào)表的一個(gè)展示效果了,本文介紹了jsp利用echarts實(shí)現(xiàn)報(bào)表統(tǒng)計(jì)的實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-10-10struts2+jquery實(shí)現(xiàn)ajax登陸實(shí)例詳解
這篇文章主要介紹了struts2+jquery實(shí)現(xiàn)ajax登陸,需要的朋友可以參考下2014-07-07JSP Spring 自動(dòng)化裝配Bean實(shí)例詳解
這篇文章主要介紹了JSP Spring 自動(dòng)化裝配Bean實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04