java ftp下载的一个小问题(人民币100元)

didiaogao 2011-09-18 10:40:25
我用org.apache.commons.net.ftp 完成了文件下载。
A机器为服务器,用B机器访问下载,文件没有下载到B机器本地。确下载到A机器里了。这个是什么原因啊
...全文
276 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
didiaogao 2011-09-19
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 liuyuhua0066 的回复:]
唯一解决办法:将文件转换成流的形式,然后返回给客户端,通过浏览器来提示用户保存
[/Quote]

这样不就是http 下载吗 ? 这样就不支持断点续传了
liuyuhua0066 2011-09-19
  • 打赏
  • 举报
回复
唯一解决办法:将文件转换成流的形式,然后返回给客户端,通过浏览器来提示用户保存
didiaogao 2011-09-19
  • 打赏
  • 举报
回复
softroad方便留个QQ吗?
softroad 2011-09-19
  • 打赏
  • 举报
回复
File file = new File(Const.getCurrentUtterlyPath() + path);
改问ftp下载的文件

softroad 2011-09-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 didiaogao 的回复:]
我用org.apache.commons.net.ftp 完成了文件下载。
A机器为服务器,用B机器访问下载,文件没有下载到B机器本地。确下载到A机器里了。这个是什么原因啊
[/Quote]

B访问下载,其实调用的是A的下载,A从ftp下载到A本地机子,然后B再去获取A的文件。

B发请求到A,A下载完文件后,A读取文件,将文件以流的方式交给浏览器,然后B就可以得到这个文件了。


protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String sufix = request.getParameter("s");
String path = request.getParameter("p") + "." + sufix;
try {
String fileName = request.getParameter("f").trim();
int c = fileName.lastIndexOf(".");
String name = fileName.substring(0, c > 0 ? c : fileName.length())
+ "." + sufix;
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachement;filename="
+ new String(name.getBytes("GBK"), "ISO-8859-1"));
File file = new File(Const.getCurrentUtterlyPath() + path);
if (!file.exists()) {
throw new IOException(fileName + ",所下载的文件不存在!");
}
response.setContentLength(Integer.parseInt(file.length() + ""));
InputStream fs = new FileInputStream(file);
OutputStream os = response.getOutputStream();
byte[] buff = new byte[1024];
int readCount = 0;
while ((readCount = fs.read(buff)) != -1) {
os.write(buff, 0, readCount);
}
if (fs != null) {
fs.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
LOG.error("error: " + e.getMessage() + ",path: " + path);
throw e;
}
response.setStatus(response.SC_OK);
response.flushBuffer();
}


你那点钱能干个屁呀
didiaogao 2011-09-19
  • 打赏
  • 举报
回复
liuyuhua0066
方便留下QQ吗
liuyuhua0066 2011-09-19
  • 打赏
  • 举报
回复
B/S下是没有办法的,换思路吧。
didiaogao 2011-09-19
  • 打赏
  • 举报
回复


/** */
/**
* 从FTP服务器上下载文件,支持断点续传
*
* @param remote
* 远程文件路径
* @param local
* 本地文件路径
* @return 上传的状态
* @throws Exception
*/
public DownloadStatus downloadFile(String remote, String local)
throws Exception {
ftpClient.setControlEncoding("iso-8859-1");
FtpConnect();

// 设置被动模式
ftpClient.enterLocalPassiveMode();
// 设置以二进制方式传输
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatus result = null;
// 检查远程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(
remote.getBytes("utf-8"), "iso-8859-1"));
if (files.length != 1) {
//System.out.println("远程文件不存在");
return DownloadStatus.Remote_File_Noexist;
}

long lRemoteSize = files[0].getSize();
File f = new File(local + remote);
this.createDir(local + remote);
// 本地存在文件,进行断点下载
if (f.exists()) {
long localSize = f.length();
// 判断本地文件大小是否大于远程文件大小
if (localSize >= lRemoteSize) {
//System.out.println("本地文件大于远程文件,下载中止");
return DownloadStatus.Local_Bigger_Remote;
}

// 进行断点续传,并记录状态
FileOutputStream out = new FileOutputStream(f, true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String(remote
.getBytes("utf-8"), "iso-8859-1"));
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);
}
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if (isDo) {
result = DownloadStatus.Download_From_Break_Success;
} else {
result = DownloadStatus.Download_From_Break_Failed;
}
} else {
OutputStream out = new FileOutputStream(f);
InputStream in = ftpClient.retrieveFileStream(new String(remote
.getBytes("utf-8"), "iso-8859-1"));
byte[] bytes = new byte[1024];
int c;
while ((c = in.read(bytes)) != -1) {
out.write(bytes, 0, c);

}
in.close();
out.close();

boolean upNewStatus = ftpClient.completePendingCommand();
this.disconnect();
if (upNewStatus) {
result = DownloadStatus.Download_New_Success;
} else {
result = DownloadStatus.Download_New_Failed;
}

}
return result;
}



是b/s的。感兴趣的 可以加我8146997
anly_hz 2011-09-19
  • 打赏
  • 举报
回复
下载和上传的方法用反了吧
anly_hz 2011-09-19
  • 打赏
  • 举报
回复
你的代码怎么写的,贴出来看看
这个问题应该很简单吧
liuyuhua0066 2011-09-19
  • 打赏
  • 举报
回复
B/S下用的FTP?
didiaogao 2011-09-19
  • 打赏
  • 举报
回复
没有能搞定吗
小哈哈 2011-09-19
  • 打赏
  • 举报
回复
一百元真不是钱了。。公司中秋发了一百块的购物卡,买了一盒避孕套,一盒口香糖,几个西红柿,韩国泡菜,没了。。。

23,409

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧