JSP file upload experience (uploaded by Javabean)
In an example of online, it is found that the content in the image file changes when the picture file is uploaded, and all the FF turns 3F when the maximum change is changed.
That is to say, when the output is transmitted as a symbol? (Is this?)
Use the code as follows:
Printwriter PW = New PrintWriter (New BufferedWriter (New BufferedWriter
FileWriter ((SavePath == NULL? "": Savepath) filename))))))))))
While (i! = -1 &&! newline.startswith (boundary)) {
// The last line of the file content contains a charter character
/ / So we must check if the current line is the most
//
i = in.readline (line, 0, 1280);
IF ((i == Boundarylength 2 || i == BoundaryLength 4)
&& (next, 0, i) .startswith (boundary)))))))))
PW.Print (newline.substring (0, newline.length () - 2))
Else
PW.Print (newline);
Newline = new string (line, 0, i);
}
PW.Close ();
But I can't always, then I will replace PrintWriter with fileOutputStream, the code is as follows:
FileOutputStream PW = new fileoutputstream ((SavePath == Null? "": Savepath) filename);
// PrintWriter PW = New PrintWriter (New BufferedWriter (New BufferedWriter
// FileWriter ((SavePath == Null? "": Savepath) filename))))))))
While (i! = -1 &&! newline.startswith (boundary)) {
// The last line of the file content contains a charter character
/ / So we must check if the current line is the most
//
i = in.readline (line, 0, 1280);
IF ((i == Boundarylength 2 || i == BoundaryLength 4)
&& (next, 0, i) .startswith (boundary)))))))))
Pw.write (newline.substring (0, newline.Length () - 2) .getbytes ("ISO8859_1"));
Else
PW.WRITE (NewLine.getBytes ("ISO8859_1");
Newline = new string (line, 0, i, "ISO8859_1");
}
PW.Close ();
It is okay, the coding of the conversion must not be on, otherwise it is not working, it is really depressed, this cross-platform can not let the people who write Code liberation, one output is N plural, looking at the cold, I think about it.
I am busy with me, it is this.
Belck the original code, it is to change the code change the code. If you please ask the original author: java bean file:
Package cr.Web;
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. *;
Public clas fileuploadbean {
Private string SavePath, FilePath, FileName, ContentType
Private byte [] b;
Byte t;
PRIVATE DICTIONARY FIELDS;
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)
Return NULL;
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 browser sends a full file path and name
/ / But Linux / UNIX and Mac browsers only send file names
POS = 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 getByte (httpservletRequest request)
{
DataInputStream IS;
INT i = 0;
Try
{
IS = New DataInputStream (Request.getInputStream ());
B = new byte [request.getContentLength ()];
While (True)
{
Try
{
T = is.readbyte ();
B [I] = T;
i ;
}
Catch (EOFEXCEPTION E)
{Break;
}
Is.close ();
Catch (IOException E)
{}
}
Public Void Doupload1 (httpservletRequest Request) THROWS
IOEXCEPTION {
Byte [] line = new byte [128];
FileOutputStream Os = New FileoutputStream ("C: //Demo.out");
ServletinputStream in = request.getinputStream ();
getByte (Request);
String temp = "";
Temp = New String (B, "ISO8859_1");
BYTE [] IMG = Temp.getbytes ("ISO8859_1");
For (int i = 0; i {os.write (IMG [i]);} Os.Close (); } Public void dolpload (httpservletRequest request) throws oException { Request.SetCharacterencoding ("GB2312"); ServletinputStream in = request.getinputStream (); Byte [] line = new byte [1280]; INT i = in.readline (line, 0, 1280); IF (i <3) Return; INT boundarylength = i - 2; String boundary = new string (line, 0, bitcharylength); // - 2 Discard charm Fields = new hashtable (); While (i! = -1) { String newline = new string (line, 0, i); IF ("Content-Disposition: Form-Data; Name = /")) { IF ("filename = /")! = -1) { SetFileName (New String (Line, 0, I-2)); IF (filename == NULL) Return; //document content i = in.readline (line, 0, 1280); SetContentType (New String (Line, 0, I-2); i = in.readline (line, 0, 1280); // i = in.readline (line, 0, 1280); Newline = new string (line, 0, i, "ISO8859_1"); FileOutputStream PW = new fileoutputstream ((SavePath == Null? "": Savepath) filename); // PrintWriter PW = New PrintWriter (New BufferedWriter (New BufferedWriter // FileWriter ((SavePath == Null? "": Savepath) filename))))); while (i! = -1 &&! Newline.startswith (boundary)) { // The last line of the file content contains a charter character / / So we must check if the current line is the most // i = in.readline (line, 0, 1280); IF ((i == Boundarylength 2 || i == BoundaryLength 4) && (next, 0, i) .startswith (boundary))))))))) Pw.write (newline.substring (0, newline.Length () - 2) .getbytes ("ISO8859_1")); Else PW.WRITE (NewLine.getBytes ("ISO8859_1"); Newline = new string (line, 0, i, "ISO8859_1"); } PW.Close (); } Else { // Ordinary form input element // Get the name of the input element INT POS = newline.indexof ("name = /" "); String FieldName = newline.substring (POS 6, Newline.Length () - 3); i = in.readline (line, 0, 1280); i = in.readline (line, 0, 1280); Newline = new string (line, 0, i); StringBuffer FieldValue = New StringBuffer (1280); While (i! = -1 &&! newline.startswith (boundary)) { // The last line contains a charter character / / So we must check if the current line is the last line i = in.readline (line, 0, 1280); IF ((i == Boundarylength 2 || i == BoundaryLength 4) && (next, 0, i) .startswith (boundary))))))))) FieldValue.Append (Newline.Substring (0, Newline.Length () - 2)); Else FieldValue.Append (newline); Newline = new string (line, 0, i); } Fields.Put (FieldName, FieldValue.toString ()); } } i = in.readline (line, 0, 1280); } } } HTML page:
hEAD>