서블릿을 사용하여 서버에서 컴퓨터로 일부 파일을 다운로드하고 있습니다. 서블릿은 MIME 유형 및 기타 사항을 처리 한 다음 대화 상자를 열고 파일 (열기, 저장, 취소)과 관련된 작업을 시작합니다. FireFox에서 사용할 때 제대로 작동하지만 IE6에서 사용할 때 파일이 다운로드되지만 내 서블릿의 URL과 404 오류가있는 새 창이 나타납니다. 나는 웹을보고 있지만, 이유에 대한 단서를 찾지 못했습니다.서블릿 : 파일을 다운로드 할 때 IE6에서 http 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();
}
}
보다 효율적으로 할 수있는 몇 가지 차별화
가능한 경우 링크가 있습니까? – Grumpy