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
}
}