日期:2014-05-20  浏览次数:20770 次

IE6下保存时文件名称时文件名不一致
<body>
  
  <a href="downloadfile.action" >下载</a>
   
  </body>
package org.jin.action ;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;


public class uploadToolAtion extends ActionSupport{
public void downloadTool(){
HttpServletResponse response = ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
try {
// application/x-msdownload application/octet-stream x-exe exe
response.setContentType("application/octet-stream");
// response.setHeader("Content-disposition ", "attachment;filename= "+ "chrome_installer.exe");
// String filename = "chrome_installer.exe" ;
String filepath = request.getRealPath("/")+"upload"+File.separator+"chrome_installer.exe";
File file = new File(filepath);
String filename = file.getName();
response.setHeader("Content-disposition ","attachment;filename="+new String(filename.getBytes("UTF-8"),"iso-8859-1"));
FileInputStream input = new FileInputStream(filepath);
long filelength = filepath.length();
String downloadFileLength = String.valueOf(filelength);
response.setHeader("Content_length", downloadFileLength);
int filelen = input.available();
byte a[] = new byte[filelen] ;
int len = 0 ;
OutputStream output = response.getOutputStream();
while ((len=input.read(a))!=-1){
  output.write(a, 0, len);
  output.flush();
  output.close();
  }
} catch (Exception e) {
}
}
}

application/x-msdownload application/octet-stream x-exe exe
response.setContentType("application/octet-stream");
为什么我换了好几种application ,IE6下保存时文件名称时文件名称老是我链接的那个Href里面的名称呀?为什么不是这定义的chrome_installer.exe呢?真纠结!

------解决方案--------------------
用firebug 跟踪一下
1 看看其它程序是什么文件头
2 看看你程序发来的是什么文件头