import org.apache.commons.net.ftp.FTPClient;
方法一、
/**
* 上传文件
*
* @param pathname ftp服务保存地址
* @param fileName 上传到ftp的文件名
* @param originfilename 待上传文件的名称(绝对地址) *
* @return
*/
public boolean uploadFile(String pathname, String fileName, String originfilename) {
boolean flag = false;
InputStream inputStream = null;
try {
System.out.println("开始上传文件");
inputStream = new FileInputStream(new File(originfilename));
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
CreateDirecroty(pathname);
ftpClient.makeDirectory(pathname);
ftpClient.changeWorkingDirectory(pathname);
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
flag = true;
System.out.println("上传文件成功");
} catch (Exception e) {
System.out.println("上传文件失败");
e.printStackTrace();
} finally {
InterruptFtpConnect();
InterruptStreamConnect(inputStream, null);
}
return true;
}
方法二、
/**
* 上传文件
*
* @param pathname ftp服务保存地址
* @param fileName 上传到ftp的文件名
* @param inputStream 输入文件流
* @return
*/
public boolean uploadFile(String pathname, String fileName, InputStream inputStream) {
boolean flag = false;
try {
System.out.println("开始上传文件");
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
CreateDirecroty(pathname);
ftpClient.makeDirectory(pathname);
ftpClient.changeWorkingDirectory(pathname);
ftpClient.storeFile(fileName, inputStream);
inputStream.close();
ftpClient.logout();
flag = true;
System.out.println("上传文件成功");
} catch (Exception e) {
System.out.println("上传文件失败");
e.printStackTrace();
} finally {
InterruptFtpConnect();
InterruptStreamConnect(inputStream, null);
}
return true;
}
方法三、 重点,返回上传进度
/**
* 上传文件到服务器,实时返回上传进度
* @param pathname 保存的文件夹路径
* @param remoteFile 文件名
* @param localFile 文件
* @param pMap 保存上传进度变量的map
* @throws IOException
*/
public void uploadFile(String pathname, String remoteFile, File localFile, Map<String,Object> pMap) throws IOException {
ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
CreateDirecroty(pathname);
ftpClient.makeDirectory(pathname);
ftpClient.changeWorkingDirectory(pathname);
//显示进度的上传
long localreadbytes = 0L; //已上传文件大小
RandomAccessFile raf = new RandomAccessFile(localFile, "r");
OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"), "iso-8859-1"));
byte[] bytes = new byte[1024];
int c;
long defaultReadPercent = 0;
while ((c = raf.read(bytes)) != -1) {
out.write(bytes, 0, c);
localreadbytes += c;
//计算上传进度
long totalSize = Long.parseLong(pMap.get("totalSize").toString());
long readSize = Long.parseLong(pMap.get("readSize").toString());
long addSize = localreadbytes - defaultReadPercent;
defaultReadPercent = localreadbytes;
readSize = readSize + addSize;
BigDecimal b_readSize = new BigDecimal(String.valueOf(readSize));
BigDecimal b_totalSize = new BigDecimal(String.valueOf(totalSize));
BigDecimal b_oneHundred = new BigDecimal(String.valueOf("100"));
BigDecimal b_readPercent = b_readSize.divide(b_totalSize, 10, BigDecimal.ROUND_DOWN).multiply(b_oneHundred);
b_readPercent = b_readPercent.setScale(0, BigDecimal.ROUND_DOWN);
pMap.put("readSize", readSize);
pMap.put("readPercent", Long.valueOf(b_readPercent.toString()));
System.out.println("--文件总大小:"+pMap.get("totalSize") +"--已上传文件大小:"+pMap.get("readSize")+"--完成百分比"+pMap.get("readPercent"));
}
out.flush();
raf.close();
out.close();
boolean result = ftpClient.completePendingCommand();
}
输出结果:
--文件总大小:1825364--已上传文件大小:1814310--完成百分比99
--文件总大小:1825364--已上传文件大小:1815334--完成百分比99
--文件总大小:1825364--已上传文件大小:1816358--完成百分比99
--文件总大小:1825364--已上传文件大小:1817382--完成百分比99
--文件总大小:1825364--已上传文件大小:1818406--完成百分比99
--文件总大小:1825364--已上传文件大小:1819430--完成百分比99
--文件总大小:1825364--已上传文件大小:1820454--完成百分比99
--文件总大小:1825364--已上传文件大小:1821478--完成百分比99
--文件总大小:1825364--已上传文件大小:1822502--完成百分比99
--文件总大小:1825364--已上传文件大小:1823526--完成百分比99
--文件总大小:1825364--已上传文件大小:1824550--完成百分比99
--文件总大小:1825364--已上传文件大小:1825364--完成百分比100
说明 也是重点
//伪代码
public class ServiceImpl implements Service {
//获取上传进度的标识
public volatile boolean goProgess = true;
/**
* 上传文件 并记录进度
* @param files
* @param folderName
* @param request
* @return
* @throws Exception
*/
public List<FileInfo> ftpUploadMarkProgress(MultipartFile[] files, String folderName){
this.goProgess = true; //线程结束标识
//初始化上传进度
Map<String, Object> pMap = new HashMap<>();
try {
setProgessInfo(files, pMap);
} catch (Exception e) {
e.printStackTrace();
}
//监听上传进度
ProgressThread thread = new ProgressThread();
thread.setMap(pMap);
thread.start();
List<FileInfo> returnList = new ArrayList<>();
for (MultipartFile file : files) {
//利用java的值引用机制, 实时获取上传进度
ftpUtils.uploadFile(uploadPath, sourceName, file, pMap);
}
this.goProgess = false;
return returnList;
}
/**
* 内部类-监听上传进度的线程
*/
class ProgressThread extends Thread{
public Map<String,Object> map;
public void setMap(Map<String,Object> map){
this.map = map;
}
@Override
public void run() {
while (goProgess){
long perenct = Long.parseLong(map.get("readPercent").toString());
if(perenct==100L){
goProgess = false;
}
//进度 存入redis
redisTemplate.opsForValue().set("readPercent", perenct);
}
}
}
}
参考这里:
上传文件、汇报进度
通过监听的方式获取进度 (我没研究)