It is said that the old point, so the code is more cumbersome, mainly used to deal with the file too much place.
SERVLET3.0 part of the next operation.
Form.html:
<!DOCTYPE HTML><HTML><Head> <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"></Head><Body> <formMethod= "POST"Action= "Upload.do"enctype= "Multipart/form-data">File:<inputtype= "File"name= "FileName"value="" /><BR> <inputtype= "Submit"value= "Upload"name= "Upload" /> </form></Body></HTML>
Uploadservlet.java:
PackageCc.openhome;ImportJava.io.DataInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Servlet Implementation class Uploadservlet*/@WebServlet ("/upload.do") Public classUploadservletextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; /** * @seeHttpservlet#httpservlet ()*/ PublicUploadservlet () {Super(); //TODO auto-generated Constructor stub } /** * @seeHttpservlet#dopost (httpservletrequest request, httpservletresponse response)*/ protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {//TODO auto-generated Method Stub byte[] BODY =readbody (Request); String TextBody=NewString (Body, "iso-8859-1"); String filename=GetFileName (TextBody); Position P=getfileposition (Request, TextBody); WriteTo (filename, body, p); } classPosition {intbegin; intend; Position (intBeginintend) { This. Begin =begin; This. end =end; } } Private byte[] Readbody (HttpServletRequest request)throwsioexception{intFormdatalength =request.getcontentlength (); DataInputStream DataStream=NewDataInputStream (Request.getinputstream ()); byteBody[] =New byte[Formdatalength]; intTotalBytes = 0; while(TotalBytes <formdatalength) { intbytes =Datastream.read (Body, totalbytes, formdatalength); TotalBytes+=bytes; } returnbody; } PrivatePosition getfileposition (httpservletrequest request, String TextBody)throwsIOException {String ContentType=Request.getcontenttype (); String Boundarytext=contenttype.substring (Contenttype.lastindexof ("=") + 1, Contenttype.length ()); intpos = Textbody.indexof ("filename=\" "); POS= Textbody.indexof ("\ n", POS) + 1; POS= Textbody.indexof ("\ n", POS) + 1; POS= Textbody.indexof ("\ n", POS) + 1; intBoundaryloc = Textbody.indexof (Boundarytext, POS)-4; intBegin = ((textbody.substring (0, POS)). GetBytes ("Iso-8859-1"). length; intEnd = ((textbody.substring (0, Boundaryloc)). GetBytes ("Iso-8859-1"). length; return NewPosition (begin, end); } Privatestring 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 ("\" ")); returnfilename; } Private voidWriteTo (String filename,byte[] body, Position p)throwsFileNotFoundException, IOException {fileoutputstream FileOutputStream=NewFileOutputStream ("c:/workspace/" +filename); Fileoutputstream.write (Body, P.begin, (P.end-p.begin)); Fileoutputstream.flush (); Fileoutputstream.close (); }}
Use getInputStream for file uploads in Servlets