This article describes how JSP implements fast uploading of files. Share it for your reference. The details are as follows:
Here we demonstrate that JSP does not use third-party libraries to realize the function of quickly uploading files
1. :
package FileUpload; import ; import ; import ; import ; /** * */ /** * @author Qch * */ public class FileUpload { ServletInputStream in=null; String fpath="C://"; public FileUpload() { fpath="C://"; in=null; } public void setInputStream(ServletInputStream in) { =in; } public void setFpath(String p) { =p; } public String getFpath() { return fpath; } public String getParameter() { String r=null; try { r=getParameter(in); } catch (Exception e) { (); } return r; } public long getFileUpload() { long r=-1; try { r=getFileUpload(in,fpath); } catch (Exception e) { (); } return r; } public String getParameter(ServletInputStream in)// Only extracted in order throws Exception { int l = 0; byte[] b = new byte[1024]; l = (b, 0, );// The start character of the property is read, name, type of attribute value, and value of attribute String si = new String(b); if (("----------------------------")) {// means that it starts reading from the start character, otherwise it should be a property after the file is just read. You should read it less at this time l = (b, 0, ); } l = (b, 0, ); l = (b, 0, ); String value = new String(b, 0, l); return value; } public long getFileUpload(ServletInputStream in, String fpath)// Input stream and storage path are required throws Exception { // ("File Information:<br>"); long begin = ();// The transmission time starts int l = 0; byte[] b = new byte[1024]; l = (b, 0, ); String sign = new String(b, 0, l);// eg.-----------------------------7d9dd29630a34 l = (b, 0, ); String info = new String(b, 0, l);// -Disposition:form-data; // name="file"; l = (b, 0, ); // String type=new // String(b,0,l);//-Type:application/octet-stream(program file) l = (b, 0, ); // String nulll=new String(b,0,l);//This value should be empty int nIndex = ().indexOf("filename=\""); int nLastIndex = ().indexOf("\"", nIndex + 10); String filepath = (nIndex + 10, nLastIndex); int na = ("\\"); String filename = (na + 1); // ("Absolute path of file: "+filepath+"<br>"); // ("Filename: "+filename+"<br><br>"); String path=fpath + filename; File fi = new File(path);// Create target file if (!()&&!()) return -2; BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream( fi)); while ((l = (b, 0, )) > 0) { if (l == ()) { String sign1 = new String(b, 0, ()); // (sign1+"<br>"); if ((sign))// Compare whether the file has been transmitted break; } (b, 0, l); (); } (); (); long end = ();// The transmission time ends // ("Uploading the file time: "+(end-begin)+"ms<br>"); return end - begin; } }
2. :
<%@ page language="java" import=".*" pageEncoding="GB18030"%> <% String path = (); String basePath = () + "://" + () + ":" + () + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My 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=""> --> <script language="javascript"> function check() { if (document.==""){ alert("The name cannot be empty!!"); document.(); return false; } if (document.==""){ alert("The file cannot be empty!!"); return false; } return true; } </script> </head> <body> <br> <form method="post" name="form2" enctype="MULTIPART/FORM-DATA" action=""> <br> <p align="center"> &nbsp; <br> </p> <table width="530" border="1" bgcolor="#c0c0c0" align="center" height="91"> <tbody> <tr> <td valign="top" align="right"> Name <br> </td> <td valign="top"> <input type="text" name="name"> </td> </tr> <tr> <td align="right"> &nbsp; document </td> <td align="left"> &nbsp; <input type="file" name="file"> </td> </tr> <tr> <td valign="top" align="right"> document类型 <br> </td> <td valign="top" align="left"> <select size="1" name="leixing"> <option selected value="Operation"> Operation </option> <option value="Course Design"> Course Design </option> <option value="paper"> paper </option> </select> </td> </tr> <tr> <td align="right"> <input type="Submit" value="Upload" name="button2" onclick="return(check());"> </td> <td align="left"> &nbsp; <input type="reset" value="Reset" name="button3"> </td> </tr> </tbody> </table> <p> &nbsp; <br> <br> &nbsp; </p> </form> </body> </html>
3. :
<%@ page language="java" import=".*,.*" pageEncoding="GB18030"%> <% String path = (); String basePath = () + "://" + () + ":" + () + path + "/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My 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=""> --> </head> <body> <jsp:useBean scope="session" class=""/> <jsp:setProperty name="upload" value="C://" property="fpath"/> <% ServletInputStream in = (); (in); String nam = (); ("Name:" + nam + "<br><br>"); long time = (); ("The file is uploaded, and it takes a total of time:" + time + "Milliseconds<br>"); String leixing = (); ("File Type:" + leixing + "<br>"); (); %> <br> <div align="right"> <a href="">Back to homepage</a> </div> </body> </html>
I hope this article will be helpful to everyone's JSP programming.