Java servlet:下载文件(但文件未加载)时,IE6出现http 404错误,打开一个新窗口

Java servlet:下载文件(但文件未加载)时,IE6出现http 404错误,打开一个新窗口,java,servlets,internet-explorer-6,download,Java,Servlets,Internet Explorer 6,Download,我正在使用servlet将一些文件从服务器下载到计算机。servlet处理MIME类型和其他事情,然后启动对话框来处理文件(打开、保存、取消)。当在FireFox中使用时,它可以正常工作,但当与IE6一起使用时,我的文件被下载,但我得到了一个新窗口,其中包含servlet的url和404错误。我在网上搜索,但没有找到任何线索 这是我的代码,如果有帮助的话: public void doGet( HttpServletRequest req, HttpServletResponse res) t

我正在使用servlet将一些文件从服务器下载到计算机。servlet处理MIME类型和其他事情,然后启动对话框来处理文件(打开、保存、取消)。当在FireFox中使用时,它可以正常工作,但当与IE6一起使用时,我的文件被下载,但我得到了一个新窗口,其中包含servlet的url和404错误。我在网上搜索,但没有找到任何线索

这是我的代码,如果有帮助的话:

public void doGet(  HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
// envoi du fichier
BufferedInputStream bIn = null;
BufferedOutputStream bOut = null;
int bytesRead;
byte[] buffer = new byte[2048];
try {
    String nomFichier = <my_filename>;
    // encodage des caractères hors [a-Z, 0-9] pis transformation des espace(+) pour l'URL
    String path = <my_path_to_download> + StringUtils.replace(URLEncoder.encode(nomFichier, "UTF-8"), "+", "%20");

    URL url = new URL(path);
    // Test si le fichier bien présent sur le serveur
    if (((HttpURLConnection) url.openConnection()).getResponseCode() != HttpURLConnection.HTTP_OK) {
        throw new FileNotFoundException();
    }
    bIn = new BufferedInputStream(url.openStream());

    String extension = StringUtils.getFilenameExtension(nomFichier).toLowerCase();
    // choix du type MIME selon l'extension du fichier
    if (Extensions.PDF.equals(extension)) {
        res.setContentType("application/pdf");
    } else if (Extensions.WORD.equals(extension)) {
        res.setContentType("application/msword");
    } else if (Extensions.HTM.equals(extension)) {
        res.setContentType("text/htm");
    } else if (Extensions.HTML.equals(extension)) {
        res.setContentType("text/html");
    } else {
        LOGGER.error("Format de fichier non géré : " + extension);
        throw new RuntimeException("Format de fichier non géré : " + extension);
    }
    // n'ouvre pas dans le navigateur et renseigne le nom de fichier pour "Save as..."
    res.setHeader("Content-Disposition", "attachment; filename=\"" + nomFichier + "\";");

    // lecture/écriture des données
    bOut = new BufferedOutputStream(res.getOutputStream());
    while (-1 != (bytesRead = bIn.read(buffer, 0, buffer.length))) {
        bOut.write(buffer, 0, bytesRead);
    }
} catch (Exception e) {
    LOGGER.error("Erreur à la récupération du fichier " + nomFichier, e.getCause());
    res.sendError(HttpServletResponse.SC_NOT_FOUND);
} finally {
    if (bIn != null)
        bIn.close();
    if (bOut != null)
        bOut.close();
}
public void doGet(HttpServletRequest-req,HttpServletResponse-res)抛出ServletException,IOException{
//费希尔环境
BufferedInputStream bIn=null;
BufferedOutputStream bOut=null;
int字节读取;
字节[]缓冲区=新字节[2048];
试一试{
字符串nomFichier=;
//编码caractères hors[a-Z,0-9]pis转换des espace(+)pour l'URL
字符串路径=+StringUtils.replace(urlcoder.encode(nomFichier,“UTF-8”),“+”,“%20”);
URL=新URL(路径);
//测试是在服务器上发送的
if(((HttpURLConnection)url.openConnection()).getResponseCode()!=HttpURLConnection.HTTP_确定){
抛出新的FileNotFoundException();
}
bIn=新的BufferedInputStream(url.openStream());
字符串扩展名=StringUtils.getFilenameExtension(nomFichier.toLowerCase();
//类型MIME selon l'扩展du fichier
if(Extensions.PDF.equals(extension)){
res.setContentType(“application/pdf”);
}else if(扩展名.WORD.equals(扩展名)){
res.setContentType(“应用程序/msword”);
}else if(Extensions.HTM.equals(extension)){
res.setContentType(“text/htm”);
}else if(Extensions.HTML.equals(extension)){
res.setContentType(“文本/html”);
}否则{
LOGGER.error(“非géré格式:“+扩展名”);
抛出新的RuntimeException(“非géré格式:”+扩展名);
}
//在“另存为…”这句话中,我们可以看到航海者和航海者的名字
res.setHeader(“内容处置”、“附件;文件名=\”“+nomFichier+“\”;”);
//演讲/演讲标准
bOut=新的BufferedOutputStream(res.getOutputStream());
而(-1!=(bytesRead=bIn.read(buffer,0,buffer.length))){
写(缓冲区,0,字节读);
}
}捕获(例外e){
LOGGER.error(“Erreurála récupération du fichier”+nomFichier,e.getCause());
res.sendError(未找到HttpServletResponse.SC);
}最后{
如果(bIn!=null)
bIn.close();
如果(大约!=null)
about.close();
}

}

除了一些可以更有效地完成的事情(例如
ServletContext#getMimeType()
等等),代码看起来很好(如果需要,您可以从中获得一些想法)

问题显然更多地出现在客户端。如果不先触发HTTP请求,就不可能获得HTTP(404)响应。客户端显然向服务器端触发了两个HTTP请求,而不是一个。造成这种情况的一个可能原因是,由于Javascript代码中的某些IE6特定错误,您在链接的
onclick
或按钮中添加了一些Javascript函数,而该按钮没有从中返回
false
。另一个可能的原因是您在链接或表单中使用了
target=“\u blank”
,而这对于下载链接/按钮来说是完全不必要的。另一个原因是您使用Javascript
window.open()
来触发下载请求,而这也是完全不必要的


毕竟,只要有一个
action
指向servlet的“普通”表单或一个
href
指向servlet的“普通”链接就可以了。
Content-Disposition:attachment
标题将强制浏览器弹出另存为对话框。这里绝对不需要Javascript或
target
属性。

除了一些可以更有效地完成的事情(例如
ServletContext\getMimeType()
等等),代码看起来很好(如果需要,您可以从中获得一些想法)

问题显然更多地出现在客户端。如果不先触发HTTP请求,就不可能获得HTTP(404)响应。客户端显然向服务器端触发了两个HTTP请求,而不是一个。造成这种情况的一个可能原因是,由于Javascript代码中的某些IE6特定错误,您在链接的
onclick
或按钮中添加了一些Javascript函数,而该按钮没有从中返回
false
。另一个可能的原因是您在链接或表单中使用了
target=“\u blank”
,而这对于下载链接/按钮来说是完全不必要的。另一个原因是您使用Javascript
window.open()
来触发下载请求,而这也是完全不必要的


毕竟,只要有一个
action
指向servlet的“普通”表单或一个
href
指向servlet的“普通”链接就可以了。
Content-Disposition:attachment
标题将强制浏览器弹出另存为对话框。这里绝对不需要Javascript或
target
属性。

如果可能的话,你有链接吗?如果可能的话,你有链接吗?好的,问题不是来自servelt,而是我调用servlet的方式:我使用的是navigateToUrl(Flex),因此它会在一个新窗口中打开。所以这是正常的,我有一个问题,因为它是空的,所以它是FF,关闭新窗口时动作不正确。由于FileReference.download()的新安全策略,我们使用了navigateToUrl,它不喜欢调用间接。。。但是我们已经找到了解决方法,现在一切都很好:)非常感谢这个答案,它指引了我正确的方向好的,问题不是来自servelt,而是我调用servlet的方式:我使用的是Navigate