JSP file upload experience (uploaded by Javabean)

xiaoxiao2021-03-06  67

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:

file upload </ title></p> <p></ hEAD></p> <p><body></p> <p><form action = jsp1.jsp encType = "Multipart / form-data" method = post></p> <p>Author: <input type = text name = author> <br></p> <p>Company: <Input Type = Text Name = Company></p> <p><br></p> <p>Description: <textarea name = comment> </ textarea></p> <p><br></p> <p>Select the file to upload <input type = file name = filename></p> <p><br></p> <p>Document Description: <Input Type = Text Name = Description></p> <p><br></p> <p><Input Type = Submit Value = "Upload"></p> <p></ form></p> <p></ body></p> <p></ html></p> <p>JSP page:</p> <p><% @ Page ContentType = "text / html; charset = GB2312"%></p> <p><jsp: usebean id = "thebean" scope = "page" class = "cr.web.fileUploadbean" /></p> <p><%</p> <p>TheBean.setsavePath ("D: //");</p> <p>//Thebean.doupload1(Request);</p> <p>TheBean.doupload (Request);</p> <p>Out.println ("FileName:" theBean.getFileName ());</p> <p>Out.println ("<br> Content Type:" TheBean.getContentType ());</p> <p>out.println ( "<BR> Author:" TheBean.getFieldValue ( "Author"));</p> <p>Out.println ("Company:" TheBean.GetfieldValue ("Company");</p> <p>Out.println ("<br> Description:" TheBean.GetfieldValue ("Comment"));</p> <p>%></p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-89707.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="89707" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.054</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = '_2B0W8qe21wDZbtfli0oOQW_2BemOVBVUE8_2BR_2FIIfdKzQyXp2zUHjpL08x_2FpgctwZv4LWl6wAUGL8urQqm0EHz4CRw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>