SoFunction
Updated on 2025-04-10

JSP implements the method of uploading files quickly

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 (!()&amp;&amp;!())
      return -2;
    BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(
        fi));
    while ((l = (b, 0, )) &gt; 0)
    {
      if (l == ())
      {
        String sign1 = new String(b, 0, ());
        // (sign1+"&lt;br&gt;");
        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. :

&lt;%@ page language="java" import=".*" pageEncoding="GB18030"%&gt;
&lt;%
  String path = ();
  String basePath = () + "://"
      + () + ":" + ()
      + path + "/";
%&gt;
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;base href="&lt;%=basePath%&gt;"&gt;
    &lt;title&gt;My JSP '' starting page&lt;/title&gt;
    &lt;meta http-equiv="pragma" content="no-cache"&gt;
    &lt;meta http-equiv="cache-control" content="no-cache"&gt;
    &lt;meta http-equiv="expires" content="0"&gt;
    &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;
    &lt;meta http-equiv="description" content="This is my page"&gt;
    &lt;!--
  &lt;link rel="stylesheet" type="text/css" href=""&gt;
  --&gt;
    &lt;script language="javascript"&gt;
      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;
      }
    &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;br&gt;
    &lt;form method="post" name="form2" enctype="MULTIPART/FORM-DATA"
      action=""&gt;
      &lt;br&gt;
      &lt;p align="center"&gt;
        &amp;nbsp;
        &lt;br&gt;
      &lt;/p&gt;
      &lt;table width="530" border="1" bgcolor="#c0c0c0" align="center"
        height="91"&gt;
        &lt;tbody&gt;
          &lt;tr&gt;
            &lt;td valign="top" align="right"&gt;
              Name
              &lt;br&gt;
            &lt;/td&gt;
            &lt;td valign="top"&gt;
              &lt;input type="text" name="name"&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td align="right"&gt;
              &amp;nbsp; document
            &lt;/td&gt;
            &lt;td align="left"&gt;
              &amp;nbsp;
              &lt;input type="file" name="file"&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td valign="top" align="right"&gt;
              document类型
              &lt;br&gt;
            &lt;/td&gt;
            &lt;td valign="top" align="left"&gt;
              &lt;select size="1" name="leixing"&gt;
                &lt;option selected value="Operation"&gt;
                  Operation
                &lt;/option&gt;
                &lt;option value="Course Design"&gt;
                  Course Design
                &lt;/option&gt;
                &lt;option value="paper"&gt;
                  paper
                &lt;/option&gt;
              &lt;/select&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
          &lt;tr&gt;
            &lt;td align="right"&gt;
              &lt;input type="Submit" value="Upload" name="button2" onclick="return(check());"&gt;
            &lt;/td&gt;
            &lt;td align="left"&gt;
              &amp;nbsp;
              &lt;input type="reset" value="Reset" name="button3"&gt;
            &lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;p&gt;
        &amp;nbsp;
        &lt;br&gt;
        &lt;br&gt;
        &amp;nbsp;
      &lt;/p&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;

3. :



&lt;%@ page language="java" import=".*,.*"
  pageEncoding="GB18030"%&gt;
&lt;%
  String path = ();
  String basePath = () + "://"
      + () + ":" + ()
      + path + "/";
%&gt;
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;base href="&lt;%=basePath%&gt;"&gt;
    &lt;title&gt;My JSP '' starting page&lt;/title&gt;
    &lt;meta http-equiv="pragma" content="no-cache"&gt;
    &lt;meta http-equiv="cache-control" content="no-cache"&gt;
    &lt;meta http-equiv="expires" content="0"&gt;
    &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;
    &lt;meta http-equiv="description" content="This is my page"&gt;
    &lt;!--
  &lt;link rel="stylesheet" type="text/css" href=""&gt;
  --&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;jsp:useBean  scope="session" class=""/&gt;
    &lt;jsp:setProperty name="upload" value="C://" property="fpath"/&gt;
    &lt;%
      ServletInputStream in = ();
      (in);
      String nam = ();
      ("Name:" + nam + "&lt;br&gt;&lt;br&gt;");
      long time = ();
      ("The file is uploaded, and it takes a total of time:" + time + "Milliseconds<br>");
      String leixing = ();
      ("File Type:" + leixing + "&lt;br&gt;");
      ();
    %&gt;
    &lt;br&gt;
    &lt;div align="right"&gt;
      &lt;a href=""&gt;Back to homepage&lt;/a&gt;
    &lt;/div&gt;
  &lt;/body&gt;
&lt;/html&gt;

I hope this article will be helpful to everyone's JSP programming.