FILE UPLOAD BY JSP

zhaozj2021-02-16  54

In Forum, a Lot of People Are Looking For Another Way To Upload File By JSP, Other Than Smartupload, Which is a free bean from jspsmart.com.

Although ITS Functionality Is Powerful, At Least Called "ENOUGH", The Sourcecode Is Invisible To US, Which Is A little pity of this bean.

A Few Days Ago, I Got An Article Talking About The Upload Things on ONJava.com.

AND the author provided a piece of code to import Upload Feature.

Click http://www.onjava.com/pub/a/onjava/2001/04/05/upload.html to enjoy it!

However, There Is Also A Flaw in His Code, That's it's it?

BUT DON 'TOPORT All File Formats.

Now i paste the whole source code below,

Hopefully it can be helpful to those who is interested in this problem.

BTW: my email is true @sina.com, Welcome to Send Mail and discuss with me on any java technical isssues.

CODE:

import javax.servlet.http.HttpServletRequest; import javax.servlet.ServletInputStream; import java.util.Dictionary; import java.util.Hashtable; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.FileWriter ; import java.io.IOException; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.InputStreamReader;

public class FileUploadBean {private String savePath, filepath, filename, contentType; private Dictionary fields; // Begin Rayman's modification private static final int BUF_SIZE = 256; // End Rayman's modification

Public string getFilename () {return filename;}

Public string getfilepath () {return filepath;} public void setsavepath (string savepath) {this.savepath = SavePath;}

Public String getContentType () {return contenttype;}

Public String getfieldValue (String FieldName) {if (Fields == Null || FieldName == Null) {//system.out.println (FieldName); //system.out.println ("ife"); return null; } //System.out.println (fieldname); //system.out.println (Fields.get ("company ")))); return (string) Fields.get (FieldName);}

Private void setfilename (string s) {

IF (s == null) return;

INT POS = S.indexof ("filename = /" "); if (pOS! = -1) {filepath = s.substring (POS 10, S.Length () - 1); // Windows Browsers include The full Full Path on the client // but linux / unix and mac browsers online = filepath.lastindexof ("//"); if (pOS! = -1) filename = filepath .SUBSTRING (POS 1); else filename = filepath;}}

Private void setContentType (String s) {if (s == null) return;

INT POS = s.indexof (":"); if (POS! = -1) ContentTyPE = S.SUBSTRING (POS 2, S.Length ());}

Public void dolpload (httpservletRequest request) throws oException {

ServletinputStream in = request.getinputStream ();

Byte [] line = new byte [buf_size]; int i = in.readline (line, 0, 128); if (i <3) return; int boundarylength = i - 2;

String boundary = new string (line, 0, bitcharylength); // - 2 discards the newline character fields = new hashtable ();

While (i! = -1) {string newline = new string (line, 0, i); if (newline.StartSwith ("Content-Disposition: Form-Data; Name = /")) {IF (Newline.indexof ("filename = /" ")! = -1) {setFileName (new string (line, 0, i-2)); if (filename == null) return; // this is the file content i = in.readline (Line, 0, 128); SetContentType (New String (Line, 0, I-2)); i = in.readline (Line, 0, 128); // Begin Rayman's Modification FileOutputStream Pw = New FileoutputStream ((SavePath = = NULL? "": SavePath) filename); Boolean Done = false; int K = 0; byte C1 = 0x0d; byte C2 = 0x0a;

BYTE [] BoundaryBytes (); Byte [] magickey = new byte [BoundaryBytes.length 2]; magickey [0] = C1; MAGICKEY [1] = C2; for (int T = 0; t 0) {

PW.WRITE (BUF, 0, K);

} If (c == magickey [0]) {pause = true;} else {pw.write (c);} k = 0;}} PW.Close (); // end rayman's model} else {// this Is A field // get the field name int pos = newline.indexof ("name = /" "); string fieldName = newline.substring (POS 6, NewLine.Length () - 3); //system.out. Println ("FieldName:" FieldName); // blank line i = in.readline (line, 0, 128); i = in.readline (line, 0, 128); newline = new string (line, 0, i StringBuffer FieldValue = new stringbuffer (128); while (i! = -1 &&! Newline.startswith (boundary)) {// the last line of the field // contains the new line character. // So, We NEED To Check if the current line is // the last line. i = in .readline (Line, 0, 128); IF ((i == Boundarylength 2 || i == Boundarylength 4) // 4 is Eof && (New String (line, 0, i) .startswith (Boundary) )) FieldValue.Append (Newline.Substring (0, newline.Length () - 2)); Else FieldValue.Append (newline = new string (line, 0, i);} //system.out.println ("FieldValue:" FieldValue.toString ()); Fields.Put (FieldName, FieldValue.toString ());}} i = in.readline (line, 0, 128);

} // end while}

Public fileUploadbean () {}}

转载请注明原文地址:https://www.9cbs.com/read-28048.html

New Post(0)