Describe the steps for reference.
Preparation:
You need to put the Jakarta Commons FileUpload and Jakarta Commons io packages in the lib.
My bag here is:
commons-fileupload-1.1.1.jar
commons-io-1.3.2.jar
Then configure multipartResolver in spring-servlet.xml. If uploading is not configured properly, it will be difficult to use.
<bean id="multipartResolver"> <property name="maxUploadSize"><value>100000</value></property><property name="defaultEncoding"><value>UTF-8</value></property> </bean>
Next is the page:
Pay attention to the writing of Form and the writing of file upload components.
<form action="uploadPosdetailFile.html" method="post" ENCTYPE="multipart/form-data"><div ><p><span><input id="startDateTxt" name="startDateTxt" /></span><label>Start Date: <span>(Date format:MM/dd/yyyy,eg:01/01/2014)</span></label></p><p><span><input id="endDateTxt" name="endDateTxt" /></span><label>End Date: <span>(Date format:MM/dd/yyyy,eg:12/25/2014)</span></label></p><p><span><input type="file" name="uploadFileCtrl" /></span><label>Upload file: <span>(click browser to choose)</span></label></p></div><div><input id="queryBtn" type="Submit" value="Submit" /></div></form>
Then write the processing code in the controller, pay attention to the correspondence between the parameters and the page control:
@RequestMapping(value="/uploadPosdetailFile") public String uploadPosdetailFile(@RequestParam("startDateTxt") String startDateTxt, @RequestParam("endDateTxt") String endDateTxt,@RequestParam("uploadFileCtrl") MultipartFile file,HttpServletRequest request,HttpServletResponse response){try {System.out.println("@@@@@@@@@@1.startDateTxt="+startDateTxt);System.out.println("@@@@@@@@@@2.endDateTxt="+endDateTxt);System.out.println("@@@@@@@@@@3.file="+file.getOriginalFilename());// Get the file name of the uploaded file if(file.isEmpty()==false){InputStream is=file.getInputStream();InputStreamReader isr = new InputStreamReader(is);BufferedReader br=new BufferedReader(isr); String s;while((s=br.readLine())!=null ){System.out.println(s);}br.close();isr.close();is.close();is.close();} return "/pages/posdetail/uploadposdetailresult/index.jsp";} catch (Exception e) {e.printStackTrace();logger.error(e);request.setAttribute("error", e.getClass());request.setAttribute("reason", e.getMessage());StackTraceElement[] arr=e.getStackTrace();request.setAttribute("stackTraceElements", arr);return "pages/error/index.jsp";}}