用struts上传图片,中文命名的图片无法显示

        今天用struts写另一个上传图片的小例子,写完之后进行测试。测试的过程中发现上传非中文命名的图片显示没有问题,但是上传中文命名图片后,发现上传没有问题,但是不显示。下面是我的代码(与上传无关的代码已删除,只作为思路体现):

        jsp页面:

 

		<form name="itemForm" method="post" action="item.do" enctype="multipart/form-data" id="itemForm">			
			<input type="hidden" name="itemNo" value="${item.itemNo }"/>
			<div align="center">
				
				<table width="95%" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td height="29">
							<div align="right">
								物料代码: 
							</div>
						</td>
						<td>
							${item.itemNo }
						</td>
					</tr>
					<tr>
						<td height="26">
							<div align="right">
								物料名称: 
							</div>
						</td>
						<td>
							${item.itemName }
						</td>
					</tr>
					
					<tr>
						<td height="74">
							<div align="right">
								图片: 
							</div>
						</td>
						<td>
							<img src="upload/${item.fileName}" width="85" height="49">
						</td>
					</tr>
					<tr>
						<td width="22%" height="29">
							<div align="right">
								<font color="#FF0000">*</font>选择图片: 
							</div>
						</td>
						<td width="78%">
							<input name=uploadFileName type="file" class="text1" size="40" maxlength="40">
						</td>
					</tr>
				</table>
				<hr width="97%" align="center" size=0>
				<div align="center">
					<input name="btn_upload" class="button1" type="submit"
						id="btn_upload" value="上传">
					    
					<input name="btnBack" class="button1" type="button" id="btnBack"
						value="返回" onClick="<%=basePath %>item.do">
				</div>
			</div>
		</form>

 

 

        ActionForm(get和set方法就不列出了)

 

        private String itemName;
	
	private FormFile uploadFileName;


        Action

 

 

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		//将form转换成ItemActionForm
		ItemActionForm iaf=(ItemActionForm)form;
		//通过工厂实例化物料维护类
		ItemService itemService=(ItemService)BeanFactory.getInstance().getServiceObject(ItemService.class);
		
		//获取上传的文件信息
		FormFile formFile=iaf.getUploadFileName();
		
		//创建目录
		File directory=new java.io.File(request.getSession().getServletContext().getRealPath("upload"));
		if (!directory.isDirectory()) {
			directory.mkdir();
		}
		
		//将文件写入到制定目录
		FileOutputStream fos=new FileOutputStream(request.getSession().getServletContext().getRealPath("upload")+"\\"+formFile.getFileName());
		fos.write(formFile.getFileData());	
		fos.close();
		
		//更新数据库信息
		itemService.uploadItemImage(iaf.getItemNo(), formFile.getFileName());
		
		return mapping.findForward("upload_success");
	}


        配置文件

 

 

<struts-config>

	<form-beans>
		<form-bean name="itemForm" type="com.jianxin.drp.web.forms.ItemActionForm" />		
	</form-beans>

	<action-mappings>
		<action path="/item" 
				name="itemForm" 
				type="com.jianxin.drp.web.actions.ItemAction" 
				scope="request" 				
		>			
			<forward name="upload_success" path="/item.do" redirect="true" />
		</action>
	</action-mappings>

	<message-resources parameter="MessageResources" />
</struts-config>


        可是上传中文命名的图片后,就是不能显示。这种问题的出现,是源于浏览器默认使用的发送编码与服务器的编码不一致导致的。

 

        我用的是tomcat7.0,在tomcat\conf下的server.xml,在Connector port="8080"那个标签里面加上URIEncoding="UTF-8"就行了,这是因为一般浏览器默认用的发送编码是UTF-8。

        在IE下可以这样修改,工具--》Internet选项--》高级,将下图所示选项给勾去掉,点击确定,那么在IE下就可以显示了(不建议使用)。

        



 

posted @ 2013-05-22 20:30  javawebsoa  Views(295)  Comments(0Edit  收藏  举报