当前位置: 技术问答>java相关
为什么下载的邮件附件打开时是乱码,直接打开也是一样的?
来源: 互联网 发布时间:2015-08-23
本文导语: 下面是我的代码: int buffsize = 1024*2; response.setContentType( "application/none" ); response.setHeader("Content-disposition","attachment; filename=" + fileName ); fileName=FormatData.ChineseStringToAscii(fileName); try{ URL ...
下面是我的代码:
int buffsize = 1024*2;
response.setContentType( "application/none" );
response.setHeader("Content-disposition","attachment; filename=" + fileName );
fileName=FormatData.ChineseStringToAscii(fileName);
try{
URL url1=new URL(/tech-qa-java/path fileName/index.html);
BufferedInputStream bis = new BufferedInputStream(url1.openStream());
BufferedOutputStream bos = new BufferedOutputStream( response.getOutputStream());
byte[] buff = new byte[buffsize];
int bytesRead;
while (-1!=(bytesRead=(bis.read(buff,0,buff.length)))){
bos.write(buff,0,bytesRead);
}
if (bis!=null){
bis.close();
}
if (bos!=null){
bos.close();
}
}catch(IOException ioe){
System.out.println("IOException at download file!"+ioe.getMessage());
ioe.printStackTrace();
}
int buffsize = 1024*2;
response.setContentType( "application/none" );
response.setHeader("Content-disposition","attachment; filename=" + fileName );
fileName=FormatData.ChineseStringToAscii(fileName);
try{
URL url1=new URL(/tech-qa-java/path fileName/index.html);
BufferedInputStream bis = new BufferedInputStream(url1.openStream());
BufferedOutputStream bos = new BufferedOutputStream( response.getOutputStream());
byte[] buff = new byte[buffsize];
int bytesRead;
while (-1!=(bytesRead=(bis.read(buff,0,buff.length)))){
bos.write(buff,0,bytesRead);
}
if (bis!=null){
bis.close();
}
if (bos!=null){
bos.close();
}
}catch(IOException ioe){
System.out.println("IOException at download file!"+ioe.getMessage());
ioe.printStackTrace();
}
|
不能确定是从服务器端获取出了问题,还是下载到浏览器端时解析错误!
你在下面的代码处加入输出到文件的代码,看看文件内容是不是对得,确认是
读取时就有错误还是输出时的错误:
while (-1!=(bytesRead=(bis.read(buff,0,buff.length)))){
bos.write(buff,0,bytesRead);
*** fileOut.write(buff,0,bytesRead);***
}
你在下面的代码处加入输出到文件的代码,看看文件内容是不是对得,确认是
读取时就有错误还是输出时的错误:
while (-1!=(bytesRead=(bis.read(buff,0,buff.length)))){
bos.write(buff,0,bytesRead);
*** fileOut.write(buff,0,bytesRead);***
}
|
其实取得附件非常麻烦的,你要处理不同格式的福建,编码,还有就是中文问题。
int chunk = part.getSize();//获得附件的大小,不一定很准确。
System.out.println("chunk=="+chunk);
byte [] buffer = new byte[chunk];
InputStream instream = part.getInputStream();
instream.read(buffer,0,chunk);
myFileoutputstream.write(buffer,0,chunk);
instream.close();
myFileoutputstream.close();
int chunk = part.getSize();//获得附件的大小,不一定很准确。
System.out.println("chunk=="+chunk);
byte [] buffer = new byte[chunk];
InputStream instream = part.getInputStream();
instream.read(buffer,0,chunk);
myFileoutputstream.write(buffer,0,chunk);
instream.close();
myFileoutputstream.close();
|
郵件附件本身不一定是文本文件啊!