FTPClient 上传

	public boolean uploadFile(String remoteFile,File localFile) throws IOException {
		ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
		FileInputStream fis = new FileInputStream(localFile);
		boolean flag =  ftpClient.storeFile(new String(remoteFile.getBytes("GBK"),"iso-8859-1"), fis);
		fis.close();
		return flag;
	}
	/**
	 * 上传文件到服务器,新上传和断点续传
	 * 
	 * @param remoteFile
	 *            远程文件名,在上传之前已经将服务器工作目录做了改变
	 * @param localFile
	 *            本地文件 File句柄,尽对路径
	 * @param processStep
	 *            需要显示的处理进度步进值
	 * @param ftpClient
	 *            FTPClient 引用
	 * @return
	 * @throws IOException
	 */
	public boolean uploadFile(String remoteFile, File localFile, long remoteSize)
			throws IOException {
		ftpClient.setSoTimeout(3000); 
		// UploadStatus status;
		// 显示进度的上传
		long step = localFile.length() / 100;
		long process = 0;
		long localreadbytes = 0L;
		RandomAccessFile raf = new RandomAccessFile(localFile, "r");
		OutputStream out = ftpClient.appendFileStream(remoteFile);
 
		// 断点续传
		if (remoteSize > 0) {
			ftpClient.setRestartOffset(remoteSize);
			if (step != 0) {
				process = remoteSize / step;
			}
			raf.seek(remoteSize);
			localreadbytes = remoteSize;
		}

		byte[] bytes = new byte[1024];
		int c;
		while ((c = raf.read(bytes)) != -1) {
			out.write(bytes, 0, c);
			localreadbytes += c;
			if (step != 0 && localreadbytes / step != process) {
				process = localreadbytes / step;
				// TODO 汇报上传状态
			}
		}
		out.flush();
		raf.close();
		out.close();
		
		boolean result = ftpClient.completePendingCommand();
		return result;
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值