Jsp&&servlet Study Notes (vii): Servlet handles uploaded files

Source: Internet
Author: User

Package cc.openhome;


Import Java.io.DataInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.swing.text.Position;


@WebServlet ("/upload.do")
public class Uploadservlet extends HttpServlet {


Private byte[] Readbody (HttpServletRequest request) throws ioexception{
int formdatalength = Request.getcontentlength ();

Get ServletInputStream Object
DataInputStream DataStream = new DataInputStream (Request.getinputstream ());
byte body[] = new Byte[formdatalength];
int totalbytes = 0;
while (TotalBytes < formdatalength) {
int bytes = Datastream.read (Body, totalbytes, formdatalength);
TotalBytes + = bytes;
}
return body;

}

protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
TODO auto-generated Method Stub

Read Request body
byte[] BODY = readbody (request);

Gets the string representation of all body contents
String textbody = new String (Body, "iso-8859-1");

Get the uploaded file name
String filename = GetFileName (textbody);

Get where the file starts and ends
Position p = getfileposition (request, TextBody);

Output to File
WriteTo (filename, body, p);
}


Private Position getfileposition (httpservletrequest request, String TextBody) throws ioexception{

Obtaining boundary information for a file segment
String ContentType = Request.getcontenttype ();
String Boundarytext = contenttype.substring (
Contenttype.lastindexof ("=") + 1, contenttype.length ());

Get the start and end position of the actual uploaded file
int pos = Textbody.indexof ("filename=\" ");
pos = Textbody.indexof ("\ n", POS) + 1;
pos = Textbody.indexof ("\ n", POS) + 1;
pos = Textbody.indexof ("\ n", POS) + 1;
int boundaryloc = Textbody.indexof (Boundarytext, POS)-4;
int begin = ((textbody.substring (0, POS)). GetBytes ("Iso-8859-1")). Length;
int end = ((textbody.substring (0, Boundaryloc)). GetBytes ("iso-8859-1"). Length;
return new Position (begin, end);
}


Class position{
int begin;
int end;
Position (int begin, int end) {
This.begin = begin;
This.end = end;

}


}

private string GetFileName (string reqbody) {
String filename = reqbody.substring (reqbody.indexof ("filename = \" ") + 10);
filename = filename.substring (0, Filename.indexof ("\ n"));
filename = filename.substring (filename.lastindexof ("\ \") + 1, filename.indexof ("\" "));
return filename;

}


private void WriteTo (String filename, byte[] body, Position p) throws FileNotFoundException, ioexception{
FileOutputStream FileOutputStream = new FileOutputStream ("c:/workspace/" + filename);
Fileoutputstream.write (Body, P.begin, (P.end-p.begin));



}





protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {
TODO auto-generated Method Stub
}






}

Jsp&&servlet Study Notes (vii): Servlet handles uploaded files

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.