Struts里FormFile作文件上传下载时编码问题

sxq0721 2004-06-11 05:58:05
Struts项目里采用UTF-8编码,没有问题。
但文件上传下载时,出现编码错误(仅文件名,文件内容没有错误)。
一、从客户端到Action
if(formFile != null) {
filename = new String(formFile.getFileName().getBytes() , "utf-8");
filesize = formFile.getFileSize();
input = formFile.getInputStream();//get InputStream
}
必须手工转码才能实现UTF-8的支持;
二、从服务器端到客户端
response.setContentType(CONTENT_TYPE);
response.setContentLength(new Long(vo.getFilesize()).intValue());
String saveAs = vo.getFilename().trim();
response.setHeader("Content-Disposition", String.valueOf((new StringBuffer("attachment;")).append(" filename=").append(new String(saveAs.getBytes() , "utf-8"))));
转换编码都不行
为什么呢?
...全文
348 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
UTF888 2004-06-23
  • 打赏
  • 举报
回复
如果是UTF-8
struts里就好办多了:
java.net.URLEncoder.encode("中文字符串","UTF-8")
就OK了
baijingqi 2004-06-21
  • 打赏
  • 举报
回复
enctype="multipart/form-data"已经转码了
sxq0721 2004-06-18
  • 打赏
  • 举报
回复
如果是国际化的,是不是java.net.URLEncoder.encode("???????","utf-8"));
客户端还要转码吗?交给IE做就可以了吧?
petertangh 2004-06-17
  • 打赏
  • 举报
回复
purejd(泪流满面)
应该是对的,我以前在.net上也是url编码的问题,需要urlencoder
weihy 2004-06-15
  • 打赏
  • 举报
回复
文件名不放到Header里
weihy 2004-06-15
  • 打赏
  • 举报
回复
我们是用另一个变量存文件名,比如提交时:
form.elements["fileName"].value = form.elements["formFile"].value;

action里得到fileName的值
purejd 2004-06-15
  • 打赏
  • 举报
回复
public void setHeader(java.lang.String name,
java.lang.String value)Sets a response header with the given name and value. If the header had already been set, the new value overwrites the previous one. The containsHeader method can be used to test for the presence of a header before setting its value.
Parameters:
name - the name of the header
(注意下面)
value - the header value If it contains octet string, it should be encoded according to RFC 2047 (http://www.ietf.org/rfc/rfc2047.txt)

你可以去查查RFC 2047 ,我英语不好,看起来痛苦

建议使用url编码response.setHeader("Content-Disposition",java.net.URLEncoder.encode("中文字符串","GB2312"));
在客户端解码java.net.URLDecoder.decode(...);
purejd 2004-06-15
  • 打赏
  • 举报
回复
header里面的内容是否支持utf-8编码呢
panpan221 2004-06-12
  • 打赏
  • 举报
回复
新手学习,不懂,帮你顶!!!

62,622

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧