JSP implements a Fast File Upload method. jsp uploads files.
This document describes how to use JSP to quickly upload files. Share it with you for your reference. The details are as follows:
This example demonstrates that JSP does not use a third-party library to quickly upload files.
1. FileUpload. java:
Using 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) {thi S. 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 can only be extracted in sequence {I Nt l = 0; byte [] B = new byte [1024]; l = in. readLine (B, 0, B. length); // read the attribute's start character, name, attribute value type, and attribute value String si = new String (B); if (si. startsWith ("----------------------------") {// indicates that the object is read from the start character. Otherwise, it should be an attribute after the object is read. At this time, the object should be read less once. 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 (Servl EtInputStream in, String fpath) // The input stream and storage path throws Exception {// out. println ("File Information: <br>"); long begin = System. currentTimeMillis (); // start time of transfer time 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-d Ata; // 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); // The value should be null 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 ("absolute file path:" + filepath + "<br>"); // out. println ("File name:" + filename + "<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) // check whether the break has been uploaded.} f. write (B, 0, l); f. flush ();} f. flush (); f. close (); long end = System. currentTimeMillis (); // transfer time timing ends // out. println ("When uploading a file:" + (end-begin) + "millisecond <br>"); return end-begin ;}}
2. submitFile. jsp:
Usage <% @ 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">
3. AnswerFile. jsp:
Bytes
<% @ 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">
I hope this article will help you with JSP program design.