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