方法一、
/**
* 上傳檔案
*
* @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);
}
}
}
}
參考這裡:
上傳檔案、彙報進度
通過監聽的方式擷取進度 (我沒研究)