java Web项目下载带繁体中文的文件出现乱码
public class LoadResourceAction extends BaseAction{
private String path;
private String title;
public void load(){
HttpServletResponse response = getHttpResponse();
try{
response.addHeader("Content-Disposition", "attachment;filename="+(title==null?null:new String(title.getBytes(),"utf-8")));
}catch(Exception e){
e.printStackTrace();
}
OutputStream out;
FileInputStream in = null;
try {
out = response.getOutputStream();
File file = new File(Constant.FILE_DIR+path);
if(!file.exists()){
file = getDefaultFile();
}
in = new FileInputStream(file);
byte[] tmp = new byte[1024];
int len =0;
int off =0;
while((len = in.read(tmp, off, 1024))!=-1){
out.write(tmp, off, len);
off+=off;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
private File getDefaultFile() {
String path = ServletActionContext.getServletContext().getRealPath("/images/noResource.png");
return new File(path);
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getTitle() {
return title;
}
public void setTitle(String title)throws UnsupportedEncodingException{
if(title!=null){
String ss=new String(title.getBytes("iso8859-1"),"utf-8");
this.title = ss;
}else{
this.title = title;
}
}
private String path;
private String title;
public void load(){
HttpServletResponse response = getHttpResponse();
try{
response.addHeader("Content-Disposition", "attachment;filename="+(title==null?null:new String(title.getBytes(),"utf-8")));
}catch(Exception e){
e.printStackTrace();
}
OutputStream out;
FileInputStream in = null;
try {
out = response.getOutputStream();
File file = new File(Constant.FILE_DIR+path);
if(!file.exists()){
file = getDefaultFile();
}
in = new FileInputStream(file);
byte[] tmp = new byte[1024];
int len =0;
int off =0;
while((len = in.read(tmp, off, 1024))!=-1){
out.write(tmp, off, len);
off+=off;
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in!=null){
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
private File getDefaultFile() {
String path = ServletActionContext.getServletContext().getRealPath("/images/noResource.png");
return new File(path);
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getTitle() {
return title;
}
public void setTitle(String title)throws UnsupportedEncodingException{
if(title!=null){
String ss=new String(title.getBytes("iso8859-1"),"utf-8");
this.title = ss;
}else{
this.title = title;
}
}
}
文件名在Action中看到是正常的,但是一到浏览器中就变成不能识别的文件,比如本来是一个XXX.ppt的文件,到浏览器里面就会变成xxxx_ppt,而且文件名还乱码,真的是头疼
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
问题解决了,上文中的代码不需要动,服务器里面配置文件server.xml添加<Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"
URIEncoding="UTF-8" />就可以了!!!不过,在谷歌下还会有问题,存在“(”或")"的文件名会显示成“%28”或"%29",需要在Action中做替换字符串的处理
获取到servlet的时候重新编译为utf8就行了,在post或get方法里多加一行代码就可以了
无法判断的,文件系统中不包含文件名的编码类型,只通过字符本身没法识别出big5与gb编码的区别
是啊,但有的文件名可能还会是简体的,难道要先判断一下汉字的简繁体再决定用哪种编码?
繁体中文用的不是大五码吗?