上传图片时要给from加上enctype="multipart/form-data"javascript
<form name="userInfo" method="post" action="first_submit.jsp" ENCTYPE="multipart/form-data">
表单标签中设置enctype="multipart/form-data"来确保匿名上传文件的正确编码。
以下:
java
<tr> <td height="30" align="right">上传企业营业执照图片:</td> <td><INPUT TYPE="FILE" NAME="uploadfile" SIZE="34" onChange="checkimage()"></td> </tr>
就得加ENCTYPE="multipart/form-data"。
表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码。默认状况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操做.
enctype="multipart/form-data"是上传二进制数据; form里面的input的值以2进制的方式传过去。 form里面的input的值以2进制的方式传过去,因此request就得不到值了。 也就是说加了这段代码,用request就会传递不成功,取表单值加入数据库时,用到下面的:
ios
SmartUpload su = new SmartUpload();//新建一个SmartUpload对象 su.getRequest().getParameterValues();取数组值 su.getRequest().getParameter( );取单个参数单个值
或者加入将form的属性加到struts.xml中去web
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <form-beans> <form-bean name="AdminForm" type="org.apache.struts.validator.DynaValidatorForm"> <form-property name="appios" type="org.apache.struts.upload.FormFile"/> <form-property name="file" type="org.apache.struts.upload.FormFile"/> <form-property name="image" type="org.apache.struts.upload.FormFile"/> </form-bean> </form-beans> <action-mappings> <action path="/admin" type="org.springframework.web.struts.DelegatingActionProxy" name="AdminForm" parameter="method" scope="request" validate="false"> <forward name="userinfolist" path="/web/tjix/manage/user/userinfolist.jsp" /> </action> </action-mappings> </struts-config>
后台action获取文件并存储
spring
String phs = getServlet().getServletContext().getRealPath("/");//获取本地项目路径 String path = phs+"data/resources/image/union/upload/";//服务器存储图片路径 上传的最终绝对路径 File file = new File(path); if (!file.exists()) { file.mkdirs(); } FormFile formFile = (FormFile) dvf.get("image"); String ImgNames = formFile.getFileName(); String appname = Uuid.create().toString() + ImgNames.substring(ImgNames.length() - 4);//图片名称 FileOutputStream out = new FileOutputStream(path+ "/" + appname); out.write(formFile.getFileData()); out.flush(); out.close(); //http://localhost:8080/fileUpload/p/file!upload String portrait = Constant.integral_Path+"fileUpload/p/file!upload"+appname;//图片路径存入数据库
异步须要js实现,后台获取路径数据库
多个图片能够用数组实现apache
//图片同步 data/resources/image/union/upload/ String ycpath = Constant.remote_image_server_url;//远程服务器地址 最终上传到此服务器路径 String phs = getServlet().getServletContext().getRealPath("/");//获取本地项目路径 String path = phs+"union/upload/";//服务器存储图片路径 上传的最终绝对路径 File file = new File(path); if (!file.exists()) { file.mkdirs(); } FormFile[] picpathFile = new FormFile[3]; picpathFile[0] = (FormFile) dvf.get("enterpriseimg"); picpathFile[1] = (FormFile) dvf.get("enterpriseimg2"); picpathFile[2] = (FormFile) dvf.get("enterpriseimg3"); picpathFile[3] = (FormFile) dvf.get("enterpriseimg4"); String[] picpath = new String[3]; for (int i = 0; i < 4; i++) { String ImgNames = picpathFile[i].getFileName(); if (StringUtil.isNullOrBlank(ImgNames)) { picpath[i] = ""; } else { String appname = Uuid.create().toString() + ImgNames.substring(ImgNames.length() - 4);// 图片名称 FileOutputStream out = new FileOutputStream(path + "/" + appname); out.write(picpathFile[i].getFileData()); out.flush(); out.close(); // http://localhost:8080/fileUpload/p/file!upload picpath[i] = Constant.images_Path+"union/upload/"+appname; } }
判断图片是否显示,不能显示则用其余图片默认数组
//picpath 图片路径 <c:if test="${good_list.picpath eq '' || good_list.picpath eq 'null' || good_list.picpath eq null}"> <img src="/cglib/alliance/images/goods.png" alt="${good_list.name}"> </c:if> <c:if test="${good_list.picpath ne '' && good_list.picpath ne 'null' && good_list.picpath ne null}"> <img src="${good_list.picpath}" onerror="javascript:this.src='/cglib/alliance/images/goods.png';" alt="${good_list.name}" /> <!-- <img src="${good_list.picpath}" alt="${good_list.name}">--> </c:if>