Home>

This demo jsp does not use third-party libraries,Ability to quickly upload files

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) //can only be extracted in order
      throws exception
  {
    int l=0;
    byte [] b=new byte [1024];
    l=in.readline (b, 0, b.length);//followed by the start character, name, type of attribute value, and attribute value
    string si=new string (b);
    if (si.startswith ("----------------------------"))
    {//means read from the start character,Otherwise it should be an attribute just after reading the file,Should be read less at this time
      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) //need to provide the input stream and storage path
      throws exception
  {
    //out.println ("File information:<br>");
    long begin=system.currenttimemillis ();//Transfer time starts
    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(program file)
    l=in.readline (b, 0, b.length);
    //string nulll=new string (b, 0, l);//this value should be empty
    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 ("File absolute path:" + filepath + "<br>");
    //out.println ("File name:" + filename + "<br><br>");
    string path=fpath + filename;
    file fi=new file (path);//create the target file
    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)) //compare whether the file has been transmitted
          break;
      }
      f.write (b, 0, l);
      f.flush ();
    }
    f.flush ();
    f.close ();
    long end=system.currenttimemillis ();//End of transmission time counting
    //out.println ("Time to upload a file:" + (end-begin) + "Milliseconds<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"&​​gt;
    <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 ("Name cannot be empty !!!");
          document.form2.name.focus ();
          return false;
        }
        if (document.form2.file.value == "") {
          alert ("File cannot be empty !!!");
          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">
              Name
              <br>
            </td>
            <td valign="top">
              <input type="text" name="name">
            </td>
          </tr>
          <tr>
            <td align="right">
               file
            </td>
            <td align="left">
              <input type="file" name="file">
            </td>
          </tr>
          <tr>
            <td valign="top" align="right">
              file type
              <br>
            </td>
            <td valign="top" align="left">
              <select size="1" name="leixing">
                <option selected value="job">
                  operation
                </option>
                <option value="course design">
                  Course Design
                </option>
                <option value="thesis">
                  paper
                </option>
              </select>
            </td>
          </tr>
          <tr>
            <td align="right">
              <input type="submit" value="upload" name="button2" onclick="return (check ());">
            </td>
            <td align="left">
              <input type="reset" value="reset" 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"&​​gt;
    <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 scope="session" />
    <jsp:setproperty name="upload" value="c://" property="fpath" />
    <%
      servletinputstream in=request.getinputstream ();
      upload.setinputstream (in);
      string nam=upload.getparameter ();
      out.println ("Name:" + nam + "<br><br>");
      long time=upload.getfileupload ();
      out.println ("File upload completed,Total time:"+ time +" ms<br>");
      string leixing=upload.getparameter ();
      out.println ("File type:" + leixing + "<br>");
      in.close ();
    %>
    <br>
    <div align="right">
      <a href="index.jsp">Back to homepage</a>
    </div>
  </body>
</html>
jsp
  • Previous C # method for obtaining IIS site and virtual directory information
  • Next Detailed Application of Vector in Java Programming