FileItem.getName()乱码

       如果不做任何处理,利用FileUpload相关类解析的的文件,文件名会出现中文乱码问题,所以解析前应该设置编码,如下:

        ServletFileUpload upload = new ServletFileUpload(factory);

        upload.setHeaderEncoding("UTF-8");//设成你的编码格式


 

if (ac.equals("newsadd")) { try { String title = ""; String img = ""; String note = ""; request.setCharacterEncoding("utf-8"); RequestContext requestContext = new ServletRequestContext( request); if (FileUpload.isMultipartContent(requestContext)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setRepository(new File(request .getRealPath("/upfile/") + "/")); ServletFileUpload upload = new ServletFileUpload(factory); upload.setSizeMax(100 * 1024 * 1024); List items = new ArrayList(); items = upload.parseRequest(request); title = ((FileItem) items.get(0)).getString(); title = Info.getUTFStr(title); note = ((FileItem) items.get(2)).getString(); note = Info.getUTFStr(note); FileItem fileItem = (FileItem) items.get(1); if (fileItem.getName() != null && fileItem.getSize() != 0) { if (fileItem.getName() != null && fileItem.getSize() != 0) { File fullFile = new File(fileItem.getName()); img = Info.generalFileName(fullFile.getName()); File newFile = new File( request.getRealPath("/upfile/") + "/" + img); try { fileItem.write(newFile); } catch (Exception e) { e.printStackTrace(); } } else { } } } String sql = "insert into news (title,img,note,savetime,type) " + "values('" + title + "','" + img + "','" + note + "','" + Info.getDateStr() + "','新闻')"; dao.commOper(sql); request.setAttribute("suc", "操作成功!"); go("/admin/newslist.jsp", request, response); } catch (Exception e1) { e1.printStackTrace(); request.setAttribute("error", ""); request.getRequestDispatcher("/admin/newsadd.jsp").forward( request, response); } }
06-03
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值