Spring3.0+Apache Tomcatv7.0+JDK1.7.0_u45+J2EE1.4验证通过。
需求:使用JSP页面上传文件时,同时需要传递相关参数。
1、 需要将form设置为enctype="multipart/form-data"method="post"参数;
2、 由于此时URL参数不能通过request.getParameter()获得,所以为了让@RequestMapping中设置的params="command=upload"条件生效,需要将参数直接加到表单的action中action="item.do?command=upload";
3、 在控制器处理时,使用item.isFormField(),item.getFieldName();item.getString()等方法去获取表单域属性名和参数;
JAVA类 import org.apache.commons.fileupload.DiskFileUpload; import org.apache.commons.fileupload.FileItem; @RequestMapping(value = public String upload(ItemActionForm HttpServletResponse String fullFileName = //Get real absolute path String .getRealPath("/images"); System.out.println("realPath=" + DiskFileUpload disk = try { List<FileItem> files = for (FileItem // Parse the parameter if (item.isFormField()) { System.out.println("item.isFormField()"); String field = if (field != if (field.equalsIgnoreCase("itemNo")) { itemNo = item.getString(); System.out.println("itemNo=" + } } } // parse the file data fullFileName = if (fullFileName == System.out.println("fullFileName not existed."); continue; } //Retrive the filename from full pathname. String fileName1 = .lastIndexOf("\\") + 1); File file1 = file1.getParentFile().mkdirs(); file1.createNewFile(); InputStream ins = OutputStream ous = try { byte[] int while ((len = ous.write(buffer, 0, } finally { ous.close(); ins.close(); } } } catch (Exception // TODO Auto-generated catch block e.printStackTrace(); } return }
|
JSP页面: <form <input <input |