<%@ page import= "java.io.*" %>
<%!
private static final int BUFSIZE = 2048;
/**
* Sends a file to the ServletResponse output stream. Typically
* you want the browser to receive a different name than the
* name the file has been saved in your local database, since
* your local names need to be unique.
*
* @param request The request
* @param response The response
* @param filename The name of the file you want to download.
* @param original_filename The name the browser should receive.
*/
private void doDownload( HttpServletRequest request, HttpServletResponse response,
String filename, String original_filename )
throws IOException
{
File f = new File(filename);
int length = 0;
ServletOutputStream op = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType( filename );
//
// Set the response and go!
//
//
response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
response.setContentLength( (int)f.length() );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
//
// Stream to the requester.
//
byte[] bbuf = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
}
%>
<%
String original_filename = request.getParameter("file");
// Security Isuue: User can type file=../WEB-INF/web.xml
//String filename = application.getRealPath(original_filename);
boolean error = false;
if (original_filename != null && !"".equals(original_filename) && !original_filename.startsWith("../")) {
String filename = application.getRealPath("/")+"WEB-INF/export/" + original_filename;
File file = new File(filename);
if (file.exists()) {
doDownload(request, response, filename, original_filename);
// delete the file after download
boolean deleted = file.delete();
System.out.println("File " + original_filename + " deleted: " + deleted);
} else {
error = true;
}
} else {
error = true;
}
if (error) {
response.setContentType("text/html; charset=UTF-8");
out.println("File not found: " + original_filename);
}
%>
檔案名稱 | download.jsp |
描述 | 檔案下載程式 |
檔案大小 | 3 Kbytes |
下載次數 | 186 次 |
下載 |
<%@ page import= "java.io.*" %>
<%!
private static final int BUFSIZE = 2048;
/**
* Sends a file to the ServletResponse output stream. Typically
* you want the browser to receive a different name than the
* name the file has been saved in your local database, since
* your local names need to be unique.
*
* @param request The request
* @param response The response
* @param filename The name of the file you want to download.
* @param original_filename The name the browser should receive.
*/
private void doDownload( HttpServletRequest request, HttpServletResponse response,
String filename, String original_filename )
throws IOException
{
File f = new File(filename);
int length = 0;
ServletOutputStream op = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType( filename );
//
// Set the response and go!
//
//
response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
response.setContentLength( (int)f.length() );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
//
// Stream to the requester.
//
byte[] bbuf = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
}
%>
<%
String original_filename = request.getParameter("file");
String target_filename = "";
// Security Isuue: User can type file=../WEB-INF/web.xml
//String filename = application.getRealPath(original_filename);
boolean error = false;
if (original_filename != null && !"".equals(original_filename.trim()) && !original_filename.startsWith("../")) {
target_filename = new String(original_filename.getBytes("ISO-8859-1"), "MS950");
String filename = application.getRealPath("/")+"WEB-INF/export/" + target_filename;
File file = new File(filename);
if (file.exists()) {
doDownload(request, response, filename, original_filename);
// delete the file after download
//boolean deleted = file.delete();
//System.out.println("File " + target_filename + " deleted: " + deleted);
} else {
error = true;
}
} else {
error = true;
}
if (error) {
response.setContentType("text/html; charset=MS950");
out.println("File not found: " + target_filename);
}
%>
檔案名稱 | download.jsp |
描述 | 支援中文檔名下載 |
檔案大小 | 3 Kbytes |
下載次數 | 36 次 |
下載 |
<%@ page import= "java.io.*" %>
<%!
private static final int BUFSIZE = 2048;
/**
* Sends a file to the ServletResponse output stream. Typically
* you want the browser to receive a different name than the
* name the file has been saved in your local database, since
* your local names need to be unique.
*
* @param request The request
* @param response The response
* @param filename The name of the file you want to download.
* @param original_filename The name the browser should receive.
*/
private void doDownload( HttpServletRequest request, HttpServletResponse response,
String filename, String original_filename )
throws IOException
{
File f = new File(filename);
int length = 0;
ServletOutputStream op = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType( filename );
//
// Set the response and go!
//
//
response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
response.setContentLength( (int)f.length() );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
//
// Stream to the requester.
//
byte[] bbuf = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
}
%>
<%
String original_filename = request.getParameter("file");
String target_filename = "";
// Chrome will auto escape the URL characters
String userAgent = request.getHeader("user-agent");
System.out.println(userAgent);
String charset = "MS950";
if (userAgent.indexOf("Chrome") != -1) {
charset = "UTF-8";
}
// Security Isuue: User can type file=../WEB-INF/web.xml
//String filename = application.getRealPath(original_filename);
boolean error = false;
if (original_filename != null && !"".equals(original_filename.trim()) && !original_filename.startsWith("../")) {
target_filename = new String(original_filename.getBytes("ISO-8859-1"), charset);
String filename = application.getRealPath("/")+"WEB-INF/export/" + target_filename;
File file = new File(filename);
if (file.exists()) {
doDownload(request, response, filename, original_filename);
// delete the file after download
//boolean deleted = file.delete();
//System.out.println("File " + target_filename + " deleted: " + deleted);
} else {
error = true;
}
} else {
error = true;
}
if (error) {
response.setContentType("text/html; charset=UTF-8");
out.println("File not found: " + target_filename);
}
%>
檔案名稱 | download.jsp |
描述 | 修正後的檔案下載程式 |
檔案大小 | 3 Kbytes |
下載次數 | 35 次 |
下載 |
mylipton wrote:andowson 您好
我把ISO-88591-1 後面的UTF-8 改成big5之後可以下載中文檔名的檔案了,
但很奇怪,如果我把路徑"/WEB-INF/export" 改成別的路徑,就又會出現File not found
<%@ page import="java.io.*"%>
<%!
private static final int BUFSIZE = 2048;
/**
* Sends a file to the ServletResponse output stream. Typically
* you want the browser to receive a different name than the
* name the file has been saved in your local database, since
* your local names need to be unique.
*
* @param request The request
* @param response The response
* @param filename The name of the file you want to download.
* @param original_filename The name the browser should receive.
*/
private void doDownload( HttpServletRequest request, HttpServletResponse response,
String filename, String original_filename )
throws IOException
{
File f = new File(filename);
int length = 0;
ServletOutputStream op = response.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType( filename );
//
// Set the response and go!
//
//
response.setContentType( (mimetype != null) ? mimetype : "application/octet-stream" );
response.setContentLength( (int)f.length() );
response.setHeader( "Content-Disposition", "attachment; filename=\"" + original_filename + "\"" );
//
// Stream to the requester.
//
byte[] bbuf = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1))
{
op.write(bbuf,0,length);
}
in.close();
op.flush();
op.close();
}
%>
<%
String original_filename = request.getParameter("file");
String target_filename = "";
// Chrome will auto escape the URL characters
String userAgent = request.getHeader("user-agent");
System.out.println(userAgent);
String charset = "MS950";
if (userAgent.indexOf("Chrome") != -1) {
charset = "UTF-8";
}
// Security Isuue: User can type file=../WEB-INF/web.xml
//String filename = application.getRealPath(original_filename);
boolean error = true;
if (original_filename != null && !"".equals(original_filename.trim()) && !original_filename.startsWith("../")) {
target_filename = new String(original_filename.getBytes("ISO-8859-1"), charset);
String filename = application.getRealPath("/")+"WEB-INF/export/" + target_filename;
File file = new File(filename);
if (file.exists()) {
doDownload(request, response, filename, original_filename);
// aviod java.lang.IllegalStateException: getOutputStream() has already been called for this response
out.clear();
out = pageContext.pushBody();
// delete the file after download
//boolean deleted = file.delete();
//System.out.println("File " + target_filename + " deleted: " + deleted);
error = false;
}
}
if (error) {
response.setContentType("text/html; charset=UTF-8");
out.println("File not found: " + target_filename);
}
%>
檔案名稱 | download.jsp |
描述 | 避免產生java.lang.IllegalStateException: getOutputStream() has already been called for this response |
檔案大小 | 3 Kbytes |
下載次數 | 92 次 |
下載 |