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

来源:互联网 发布:香港免备案域名 编辑:程序博客网 时间:2024/06/05 22:59

        今天用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转换成ItemActionFormItemActionForm 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下就可以显示了(不建议使用)。

        



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 车座位里面倒了汤怎么办 腿被棍子打肿了怎么办 刚买的手机碎屏怎么办 被木棍么么打到头项怎么办 大王卡用到40g怎么办 王卡40g用完了怎么办 父亲把母亲打成重伤怎么办 狗狗脖子摔歪了怎么办 吃鸡游戏中重伤怎么办 吃鸡游戏摔伤了怎么办 现在没能力偿还网贷怎么办 上海找夜场工作被骗了怎么办? 买了烂尾的楼盘怎么办 孕七个月蛀牙疼怎么办 宝宝只吃一边奶怎么办 实房付了定金后悔了怎么办 开车时遇到意外事故时怎么办 没干过影楼门市怎么办 uplay下载游戏速度0k怎么办 缓刑期间被别人打成轻伤怎么办 缓刑期间被别人打了怎么办 有缓刑被别人打怎么办 检察院不予立案怎么办公安局取保 监外执行过了又犯罪怎么办 监视居住期间再次犯罪怎么办 前妻有贷款跑了怎么办 未成年犯有缓刑罪继续犯罪怎么办 英朗出现检修esc怎么办 照相时奖状拿反了怎么办 wps文档加密后忘记密码怎么办 wps表格文件密码忘记了怎么办 wps表格密码忘记了怎么办 发布的文案缩成一行怎么办 创维电视都是VIP怎么办 挂电视距离太远怎么办 电视离沙发太近怎么办 苹果6更新11.1卡怎么办 苹果升级变卡了怎么办 迅雷被资源举报无法加速怎么办 迅雷的敏感资源无法加速怎么办 迅雷下载敏感资源无法加速怎么办