JSP & amp; SERVLET Study Notes (7): Servlet processes uploaded files. jsp Introduction

Source: Internet
Author: User

JSP & SERVLET Study Notes (7): Servlet processes uploaded files. jsp Introduction
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 ();

// Retrieve the 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 the Request Body
Byte [] body = readBody (request );

// Obtain the string representation of all Body content
String textBody = new String (body, "ISO-8859-1 ");

// Obtain the name of the uploaded file
String filename = getFilename (textBody );

// Get the start and end locations of the file
Position p = getFilePosition (request, textBody );

// Output to file
WriteTo (filename, body, p );
}


Private Position getFilePosition (HttpServletRequest request, String textBody) throws IOException {

// Obtain the boundary information of the file segment
String contentType = request. getContentType ();
String boundaryText = contentType. substring (
ContentType. lastIndexOf ("=") + 1, contentType. length ());

// Obtain the start position and end position of the actually 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
}






}

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.