publicclass Ticket{ private String customerName; private String subject; private String body; privatenew LinkedHashMap<>(); //...}
publicclass Attachment{ private String name; privatebyte[] contents; //...}
@WebServlet (name = "Ticketservlet" , Urlpatterns = {}, Loadonstartup = 1 ) @MultipartConfig (filesizethreshold = 5_242_880, //5mb maxfilesize = 20_971_520l, //20mb maxrequestsize = 41_943_040l //40MB ) //@MultipartConfig location tells the browser where to store the temporary file (not used here, let the application server use its default temp directory) . Span class= "Co" >//filesizethreshold will tell the Web container file how large it must be to write to the temporary directory (in this case, an upload file smaller than 5MB will be saved in memory.) //in this example, the MaxFileSize setting prohibits uploading files larger than 20MB in size. //in this example, the Maxrequestsize setting prohibits requests larger than 40MB in size. public class ticketservlet extends httpservlet{//... }
Public classTicketservletextendshttpservlet{//... The//downloadattachment method is used to handle download requests from the client browser. Private void downloadattachment(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {//... //The header content-disposition set in the response will force the browser to ask the customer to be //Save or download the file instead of opening it online in the browser. Response.SetHeader("Content-disposition","attachment; Filename= "+ attachment.GetName()); Response.setContentType("Application/octet-stream");//Use Servletoutputstream to output file contents to the response (there is a memory problem with large file processing)Servletoutputstream stream = Response.Getoutputstream(); Stream.Write(Attachment.getcontents()); }//...}
Public classTicketservletextendshttpservlet{//... Private void Createticket(HttpServletRequest request, httpservletresponse response)throwsServletexception, IOException {Ticket Ticket =New Ticket(); Ticket.Setcustomername(Request.GetParameter("CustomerName")); Ticket.Setsubject(Request.GetParameter("Subject")); Ticket.Setbody(Request.GetParameter("Body")); Part Filepart = Request.GetPart("File1");if(Filepart! =NULL&& Filepart.GetSize() >0) {Attachment Attachment = This.processattachment(Filepart);if(Attachment! =NULL) ticket.AddAttachment(attachment); }intIdsynchronized( This) {id = This.ticket_id_sequence++; This.Ticketdatabase.put(ID, ticket); } response.Sendredirect("Tickets?action=view&ticketid="+ ID); }PrivateAttachmentprocessattachment(Part Filepart)throwsIOException {//Get InputStream from the multipart request and copy it to the Attachment object. InputStream InputStream = Filepart.getInputStream(); Bytearrayoutputstream OutputStream =NewBytearrayoutputstream ();intReadFinal byte[] bytes =New byte[1024x768]; while(read = InputStream.Read(bytes)) != -1) {OutputStream.Write(Bytes,0, read); } Attachment Attachment =New Attachment(); Attachment.SetName(Filepart.Getsubmittedfilename()); Attachment.setcontents(OutputStream.Tobytearray());returnAttachment }//...}
The source of the article link: Https://pan.baidu.com/s/1ZnkwhlZo6_ihutdbgA7FvQ Password: q33p
Other information about file upload Source link: https://pan.baidu.com/s/1PW-sdelXD6i3S3deFmVi0g Password: V7HD
Java Web Advanced Programming Sample code: HTTP://WWW.WROX.COM/GO/PROJAVAFORWEBAPPS
Reference: "Java Web Advanced Programming", chapter 3rd, section 6th
Java ee upload file via form