这个文件上传重写了 HttpRequest,不讲费话直接写方法和代码了,对了,还有两个包不要忘记引入 1.commons-io-1.3.2.jar 2.commons-io-1.3.2.jar
前台页面:index.jsp
View Code
<form id="fileUploadForm" name = "fileUploadForm" method="post" action="physical.do?method=physicalInitSave" enctype="multipart/form-data"> <ul style="list-style: none;line-height: 30px;"> <li><input type="hidden" id="id" name="id"/></li> <li> 序 号: <input id="serialNumber" name="serialNumber" type="text" style=" 150px;"/> 所在公司: <input class="easyui-combobox" id="company" name="company" panelHeight="auto" style=" 150px;_ 108px;"/> 计划人数:<input id="planNumber" name="planNumber" type="text" style=" 150px;"/> </li> <li> 体检时间: <input id="dateTime" name="dateTime" class="easyui-datebox" style=" 150px;_108px;" /> 所在基地: <input class="easyui-combobox" id="area" name="area" panelHeight="auto" style=" 150px;_ 108px;"/> </li> <li> 实检人数: <input id="realNumber" name="realNumber" type="text" onblur="yield()" size="5" style=" 150px;"/> 合格人数: <input id="passNumber" name="passNumber" type="text" onblur="yield()" size="5" style=" 150px;"/> 合格率:<input id="endYield" name="endYield" type="text" style=" 150px;"/> </li> <li> 参检人员明细表:<input type="file" id="personnel" name="personnel" style=" 150px;"/> 医生负责人:<input id="doctor" name="doctor" type="text" style=" 150px;"/> </li> <li> 备 注: <textarea id="comment" name="comment" rows="3" cols="70"></textarea> </li> <li style="text-align: center;"> <a class="easyui-linkbutton" icon="icon-ok" onclick="fileUploadForm.submit()">保存</a> <a class="easyui-linkbutton" icon="icon-redo" id="Init_rest"> 重置</a> </li> </ul> </form>
保存:save.java 直接写的方法
View Code
/** -----------------------保存数据---------------------------------------------- */ /* 招飞初检保存 */ @RequestMapping(params = "method=physicalInitSave", method = RequestMethod.POST) //这个可以不要,是spring注解 public String physicalInitSave(HttpServletRequest request) { request = new MulpartRequestWrapper(request); //引入重写request的实体类 必须引入 Physical physicals=new Physical(); //实例化一个实体 String comment = request.getParameter("comment"); //从request 上获得内容 如:<input id="comment" name="comment" type="text"> physicals.setPersonnel(request.toString()); //request.toString() 就是文件上传的地址 physicalBO.save(physicals); //保存实体 return "physical/physicalInit"; //只是一个返回值 aa也一样 }
文件上传:MulpartRequestWrapper.java 注:本文件不用修改 直接贴过去就行了
View Code
package com.hna.aircrewhealth.medicalinfo.action; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import org.apache.commons.fileupload.FileItemIterator; import org.apache.commons.fileupload.FileItemStream; import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.util.Streams; import org.apache.commons.io.FilenameUtils; import com.hna.aircrewhealth.security.LoginUserUtil; public class MulpartRequestWrapper extends HttpServletRequestWrapper { private Map<String, String[]> map; private String path; /*private String prefix; private String suffix;*/ //private String parentPath; @SuppressWarnings("deprecation") public MulpartRequestWrapper(HttpServletRequest request) { super(request); try{ boolean isMultpart = ServletFileUpload.isMultipartContent(request); if(isMultpart){ map = new HashMap<String, String[]>(); ServletFileUpload upload = new ServletFileUpload(); FileItemIterator iters = upload.getItemIterator(request); FileItemStream item = null; path = request.getRealPath("/") + "fileUpload" + File.separator; File file = new File(path); //HttpSession session = request.getSession(); String usernameid = LoginUserUtil.getStaffCode();//(String)session.getAttribute("usernameid"); String time = DateTimeUtil.getSystemTime(); if(!file.exists() || !file.isDirectory()){ file.mkdir(); file = new File(path + File.separator + usernameid); if(!file.exists() || !file.isDirectory()){ file.mkdir(); file = new File(path + File.separator + usernameid + File.separator + time.substring(0,8)); file.mkdir(); }else{ file = new File(path + File.separator + usernameid + File.separator + time.substring(0,8)); if(!file.exists() || !file.isDirectory()){ file.mkdir(); } } }else{ file = new File(path + File.separator + usernameid); if(!file.isDirectory() || !file.exists()){ file.mkdir(); file = new File(path + File.separator + usernameid + File.separator + time.substring(0,8)); file.mkdir(); }else{ file = new File(path + File.separator + usernameid + File.separator + time.substring(0,8)); if(!file.isDirectory() || !file.exists()){ file.mkdir(); } } } //parentPath = path; while(iters.hasNext()){ item = iters.next(); String name = item.getFieldName(); InputStream in = item.openStream(); if(!item.isFormField()){ String fileName = item.getName(); if(fileName != null && !"".equals(fileName)){ fileName = FilenameUtils.getName(fileName); /*prefix = fileName.split("\\.")[0]; suffix = fileName.split("\\.")[1]; fileName = prefix + "_" + uuid.getUUID() + "." + suffix;*/ path += usernameid + File.separator + time.substring(0,8) + File.separator; path += fileName; path = path.replace("\\", "/"); Streams.copy(in, new FileOutputStream(path), true); //path = parentPath; }else{ System.out.println("文件名为空"); } }else{ String value = Streams.asString(in); this.paramsLoadToMap(name, value); } } }else{ map = request.getParameterMap(); } }catch(Exception e){ e.printStackTrace(); } } private void paramsLoadToMap(String name, String value){ String[] fieldValues = (String[])map.get(name); if(fieldValues == null){ map.put(name, new String[]{value}); }else{ fieldValues = Arrays.copyOf(fieldValues, fieldValues.length + 1); fieldValues[fieldValues.length - 1] = value; map.put(name, fieldValues); } } @Override public String getParameter(String name) { String[] values = (String[]) map.get(name); if(values != null){ return values[0]; } return null; } @Override public String[] getParameterValues(String name) { String[] values = (String[]) map.get(name); return values; } @Override public Map<String, String[]> getParameterMap() { return map; } @Override public String toString() { return this.path; } }