当struts下载的时候遇到中文乱码、中文丢失,问题如下:
(1)中文省去的问题,当我的文件是 :海洋视频.mp4,因为前边的是中文,自动省去了。
(2)中文乱码,变成乱码。 --.mp4
(3)变成了 .aciton的名字。
解决方法:
把你的文件名字加上这句话转码一下,就完美解决了。
doFileName = java.net.URLEncoder.encode(doFileName, "UTF-8");
package action; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; public class DownAction { InputStream inputStream; String fileName; public InputStream getInputStream() { return inputStream; } public void setInputStream(InputStream inputStream) { this.inputStream = inputStream; } public String getFileName() throws UnsupportedEncodingException { return fileName; } public void setFileName(String fileName) throws UnsupportedEncodingException { this.fileName = fileName; } public String execute() throws Exception{ File file=new File("D:/image/海洋找不同游戏.mp4"); //手动测试 fileName=file.getName();//获取文件名 System.out.println("刚获取的fileName:"+fileName); //转换格式 fileName = java.net.URLEncoder.encode(fileName, "UTF-8"); System.out.println("this.getFileName():"+fileName); try{ inputStream =new FileInputStream(file); }catch(Exception e){ e.printStackTrace(); } return "success"; } }
成功地显示出来了
下载文件的源代码:http://jingyan.baidu.com/album/4853e1e577056a1909f72630.html?picindex=4
下载文件的源代码参考:http://2342615.blog.51cto.com/2332615/716193/