我正在尝试创建一种方法,允许用户通过Servlet下载文件系统上的文件。现在,下载时显示正确的filename
并下载文件,但文件内容始终为空。
我还尝试将文件内容打印到控制台但没有显示。
有人能说出我在这里做错了吗?
由于
String filePath = "uploads/" + request.getParameter( "filename" );
File downloadFile = new File( filePath );
String relativePath = getServletContext().getRealPath( "" );
System.out.println( "relativePath = " + relativePath );
ServletContext context = getServletContext();
String mimeType = context.getMimeType( filePath );
if( mimeType == null )
{
mimeType = "application/octet-stream";
}
System.out.println( "MIME type: " + mimeType );
response.setContentType( mimeType );
response.setContentLength( (int) downloadFile.length() );
String headerKey = "Content-Disposition";
String headerValue = String.format( "attachment; filename=\"%s\"", downloadFile.getName() );
response.setHeader( headerKey, headerValue );
byte[] bytes = Files.readAllBytes( downloadFile.getAbsoluteFile().toPath() );
OutputStream outStream = response.getOutputStream();
System.out.println( bytes.toString() );
outStream.write( bytes );
outStream.flush();
outStream.close();
注解
@WebServlet(urlPatterns =
{ "/Routing/*" })
@MultipartConfig(location = "/tmp", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
public class Routing extends HttpServlet
{
答案 0 :(得分:2)
我建议使用几种测试来隔离问题,因为这里仍有太多未知数。我在运行代码时没有遇到任何问题,因此很可能是servlet容器的配置问题,或者是关于文件系统的假设。
隔离问题的关键是从基础开始。尝试返回String而不是文件,以确保与服务器的通信实际上正常运行。如果您得到相同的响应,那么您知道问题不在于文件IO:
byte[] bytes = "This is a test.".getBytes();
int contentLength = bytes.length;
String mimeType = "text/plain";
response.setContentType( mimeType );
response.setContentLength( contentLength );
OutputStream outStream = response.getOutputStream();
outStream.write( bytes );
outStream.flush();
outStream.close();
如果上述测试有效,那么您就知道问题出在文件或用于读取文件的方法上。您的代码目前对正在请求的文件的所有内容做出了很多假设。执行一些严格的测试以确保文件可访问:
int fileSize = downloadFile.length();
System.out.println("File path: " + downloadFile.getAbsolutePath());
System.out.println("exists: " + downloadFile.exists());
System.out.println("canRead: " + downloadFile.canRead());
System.out.println("File size: " + fileSize);
最后检查文件系统报告的文件大小与读取的字节数:
byte[] bytes = Files.readAllBytes( downloadFile.getAbsoluteFile().toPath() );
int bytesRead = bytes.length;
System.out.println("bytes read: " + bytesRead);
这些测试的结果应该为您缩小问题范围。
答案 1 :(得分:2)
此解决方案是BalusC File Servlet blog的修改后的解决方案。
我使用此解决方案的原因是因为它在写入数据之前reset()
HttpServletResponse response
。
@WebServlet(urlPatterns =
{ "/Routing/*" })
@MultipartConfig(location = "/tmp", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
public class FileServlet extends HttpServlet {
// Constants ----------------------------------------------------------------------------------
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
// Actions ------------------------------------------------------------------------------------
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
// Get requested file by path info.
String filePath = "uploads/" + request.getParameter( "filename" );
// Check if file is actually supplied to the request URI.
if (filePath == null) {
// Do your thing if the file is not supplied to the request URI.
// Throw an exception, or send 404, or show default/warning page, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Decode the file name (might contain spaces and on) and prepare file object.
File downloadFile = new File( filePath );
// Check if file actually exists in filesystem.
if (!downloadFile.exists()) {
// Do your thing if the file appears to be non-existing.
// Throw an exception, or send 404, or show default/warning page, or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Get content type by filename.
String contentType = getServletContext().getMimeType(file.getName());
// If content type is unknown, then set the default value.
// For all content types, see: http://www.w3schools.com/media/media_mimeref.asp
// To add new content types, add new mime-mapping entry in web.xml.
if (contentType == null) {
contentType = "application/octet-stream";
}
// Init servlet response.
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType(contentType);
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
// Prepare streams.
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
// Open streams.
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
// Write file contents to response.
byte[] bytes = Files.readAllBytes( downloadFile.getAbsoluteFile().toPath() );
output.write(buffer, 0, bytes.length);
} finally {
// Gently close streams.
close(output);
close(input);
}
}
// Helpers (can be refactored to public utility class) ----------------------------------------
private static void close(Closeable resource) {
if (resource != null) {
try {
resource.close();
} catch (IOException e) {
// Do your thing with the exception. Print it, log it or mail it.
e.printStackTrace();
}
}
}
}
我希望这会有所帮助。
答案 2 :(得分:0)
可能文件为空添加此行以确保文件不为空
System.out.println("File size: " + bytes.length);
答案 3 :(得分:0)
添加 outStream.setContentLength(inStream.available); 在outStream.write(bytes)之后;