This article describes the JSP implementation of fast upload file method. Share to everyone for your reference. Specifically as follows:
This demo JSP does not use the Third-party library, the realization of fast upload file function
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;
The 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);/sequentially read the start of the property, the name, the type of the property value, the value of the property string si = new string (b); if (Si.startswith ("----------------------------")) {//means to start reading from the start character, or it should be a property after the file has just been read, you should read it less once 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 input stream and storage path throws Exception {//OUT.PR
INTLN ("File Information:<br>");
Long begin = System.currenttimemillis ();//Transfer time timed start 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 an empty int nindex = Info.tolowercase (). IndexOf ("filename=\");
int nlastindex = Info.tolowercase (). IndexOf ("\", nindex + 10);
String filepath = info.substring (nindex +, nlastindex);
int na = Filepath.lastindexof ("\");
String filename = filepath.substring (Na + 1);
Out.println ("File absolute path:" +filepath+ "<br>");
OUT.PRINTLN ("FileName:" +filename+ "<br><br>");
String Path=fpath + filename;
File Fi = Object 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 St
Ring (b, 0, sign.length ());
Out.println (sign1+ "<br>"); if (Sign1.startswith (sign))//compare to whether the file has passed the break;
F.write (b, 0, L);
F.flush ();
} f.flush ();
F.close ();
Long end = System.currenttimemillis ()//delivery time timed over//out.println ("Upload file:" + (End-begin) + "millisecond <br>");
return end-begin;
}
}
2. submitfile.jsp:
<%@ page language= "java" import= "java.util.*" pageencoding= "GB18030"%> <% String Path = Request.getcontextpat
H (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path
+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
3. Answerfile.jsp:
<%@ page language= "java" import= "java.util.*,java.io.*" pageencoding= "GB18030"%> <% String Path = request.ge
Tcontextpath (); String basepath = request.getscheme () + "://" + request.getservername () + ":" + request.getserverport () + path
+ "/"; %> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
I hope this article will help you with your JSP programming.