我正在尝试使用sftp连接从web应用程序下载文件。我可以上传一个文件,但从下面的代码一个虚拟文件正在下载,但文件内没有内容。
InputStream in = channelSftp.get(myFile);
// if you want to use a relative path to context root:
String relativePath = getServletContext().getRealPath("");
System.out.println("relativePath = " + relativePath);
// obtains ServletContext
ServletContext context = getServletContext();
// gets MIME type of the file
String mimeType = context.getMimeType(path);
if (mimeType == null) {
// set to binary type if MIME mapping not found
mimeType = "application/octet-stream";
}
// modifies response
response.setContentType(mimeType);
response.setContentLength((int) downloadFile.length());
// forces download
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"",
downloadFile.getName());
response.setHeader(headerKey, headerValue);
// obtains response's output stream
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
System.out.println( in +"input streem in download file");
while ((bytesRead = in .read(buffer)) != -1) {
System.out.println(buffer + " bytesRead in download");
outStream.write(buffer, 0, bytesRead);
} in .close();
outStream.close();
在上面的代码中,我使用 InputStream in = channelSftp.get(myFile);
. 有人能认出我的错误吗?
暂无答案!
目前还没有任何答案,快来回答吧!