package int_2.servlets.assignments2; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import int_2.servlets.*; import int_2.tools.*; import int_2.beans.*; /** Please respect the copyright of this code. It is not allowed to copy this code and use at any circumstances. @author © Fredrik Andersson
This servlet uses code from my home made toolbox that you can find in the folder 8/tools and 8/beans. The toolbox is not ready yet, I have been working for it for a couple of years when I have time. The goal is to get a toolbox of classes so it will be easier to map tasks to use-case-diagram. This servlet collects a form that uploads a file. It will be saved into a folder called uploads. The maximum size of the file is 50Kb. */ public class FileUpload_2d extends GeneralServlet { public void start(HttpServletRequest request, HttpServletResponse response) throws IOException { String output = createPage("applications/int_2/int_2/templates/file_upload.html", "file_upload"); try { Form form = FormManager.collectFormWithMultiPart(request, "applications/int_2/int_2/uploads", 50000); if(form != null) { StringBuffer filePath = new StringBuffer(""); if(form.get("size") != null) { String name = (String)form.get("filename_file"); String mimeType = (String)form.get("mime-typ"); String size = ((Long)form.get("size")).toString(); if( mimeType.equals("text/plain") || mimeType.equals("image/gif") || mimeType.equals("image/pjpeg") || mimeType.equals("image/jpeg") || mimeType.equals("image/x-png") ) { filePath.append("The file"); } else { filePath.append(name); filePath.append("
"); filePath.append(mimeType); filePath.append("
"); filePath.append(size); filePath.append(" (bytes)"); } } else { output = TextManager.replaceInString(output, "XXXINFOXXX", "You must fill in all fields before you submit!"); } output = TextManager.replaceInString(output, "XXXINFOXXX", filePath.toString()); } else { output = TextManager.replaceInString(output, "XXXINFOXXX", "Upload a file"); } } catch(Exception e) { output = TextManager.replaceInString(output, "XXXINFOXXX", e.toString()); e.printStackTrace(); } printString(output); flush(); } }