上傳文件或圖片,圖片存在即顯示問題

上傳圖片時要給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>
相關文章
相關標籤/搜索