JAVA上傳FTP檔案伺服器
IFileService的實現類:
public class FileServiceImpl implements IFileService {
private static Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);
@Override
public String upload(MultipartFile file, String path) {
// 原始名稱
String filename = file.getOriginalFilename();
//副檔名
String fileExtensionName = filename.substring(filename.lastIndexOf(".") + 1);
//檔名
String uploadFileName = UUID.randomUUID().toString() + "." + fileExtensionName;
logger.info("開始上傳檔案,上傳檔案的檔名:{},上傳的路徑:{},新檔名:{}", filename, path, uploadFileName);
File fileDir = new File(path);
if (!fileDir.exists()) {
fileDir.setWritable(true);
fileDir.mkdirs();
}
// 新檔案
File targetFile = new File(path, uploadFileName);
try {
file.transferTo(targetFile);
//上傳檔案到FTP伺服器上
FTPUtil.uploadFile(Lists.newArrayList(targetFile));
//上傳完成後,刪除快取中的檔案。
targetFile.delete();
} catch (Exception e) {
logger.error("圖片上傳失敗",e);
return null;
}
return targetFile.getName();
}
}
FTPUtil工具類
public class FTPUtil {
private static Logger logger = LoggerFactory.getLogger(FTPUtil.class);
private static String ftpIp = PropertiesUtil.getProperty("ftp.server.ip");
private static String ftpUser = PropertiesUtil.getProperty("ftp.user");
private static String ftpPass = PropertiesUtil.getProperty("ftp.pass");
private String ip ;
private int port;
private String user;
private String pwd;
private FTPClient ftpClient;
public FTPUtil(String ip, int port, String user, String pwd) {
this.ip = ip;
this.port = port;
this.user = user;
this.pwd = pwd;
}
public static boolean uploadFile(List<File> fileList) throws IOException {
FTPUtil ftpUtil = new FTPUtil(ftpIp,21,ftpUser,ftpPass);
logger.info("開始連線FTP伺服器");
//放在FTP伺服器根目錄
boolean result = ftpUtil.uploadFile("/",fileList);
logger.info("開始連線FTP伺服器,結束上傳,上傳結果:{}",result);
return result;
}
private boolean uploadFile(String remotePath,List<File> fileList) throws IOException {
boolean uploaded = true;
FileInputStream fis =null;
//連線FTP伺服器
if(connectServer(this.ip,this.port,this.user,this.pwd)){
try {
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.enterLocalPassiveMode();
for(File fileItem : fileList){
fis = new FileInputStream(fileItem);
ftpClient.storeFile(fileItem.getName(),fis);
}
} catch (IOException e) {
e.printStackTrace();
logger.error("上傳檔案異常",e);
uploaded = false;
} finally {
fis.close();
ftpClient.disconnect();
}
}
return uploaded;
}
private boolean connectServer(String ip,int port ,String user,String pwd){
boolean isSuccess = false;
ftpClient =new FTPClient();
try {
ftpClient.connect(ip);
isSuccess = ftpClient.login(user,pwd);
logger.info("連線{}",isSuccess);
} catch (IOException e) {
e.printStackTrace();
logger.error("連線FTP伺服器異常",e);
}
return isSuccess;
}
public static String getFtpIp() {
return ftpIp;
}
public static void setFtpIp(String ftpIp) {
FTPUtil.ftpIp = ftpIp;
}
public static String getFtpUser() {
return ftpUser;
}
public static void setFtpUser(String ftpUser) {
FTPUtil.ftpUser = ftpUser;
}
public static String getFtpPass() {
return ftpPass;
}
public static void setFtpPass(String ftpPass) {
FTPUtil.ftpPass = ftpPass;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public FTPClient getFtpClient() {
return ftpClient;
}
public void setFtpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
}
FTP上傳檔案war包依賴
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>2.13.2</version>
</dependency>
相關推薦
JAVA上傳FTP檔案伺服器
IFileService的實現類: public class FileServiceImpl implements IFileService { private static Logger logger = LoggerFactory.getLog
vue 整合ueditor(百度富文字編輯器)以及使用後端Java上傳圖片到伺服器,特別注意的大坑
1.import 引入ueditor時,在封裝元件中引入,不要在mian.js內引入,在main.js內引入會造成 1.Uncaught SyntaxError: Unexpected token : 這種錯誤,屬於是跨域問題,目前不清楚是什麼原因和原理,
Java上傳下載檔案
前端來講用axios下載: 請求方式: export function exportUser(jsonObj) {//匯出使用者資料 return fetch({ url: '/api/frontUser/user/exportUser', metho
Java上傳Excel檔案匯入資料
Controller中接收form表單提交的檔案域: public Map importConsumer(@RequestParam("file") MultipartFile file) 讀取Excel工具類 這裡我以Consumer實體類來寫,大家可以自行封裝: pub
java上傳下載檔案部署到linux系統下的一些問題
專案遇到上傳下載,在windows系統上測試都可以,但是部署到linux上就不行,試了很多種方法,只有這種才通過了。 首先上傳 /** * 上傳 */ @RequestMapping(value="/upload",method = Reques
Java上傳大檔案的解決方案
java SmartUplaod元件支援50M以內的檔案上傳,而且其上傳方式為將檔案首先讀入記憶體。只對於記憶體的需求較大 要上傳較大的檔案,比如說1G以上的檔案,需要使用commons-fileupload-1.2.jar元件。
java+jsp程式碼實現從FTP伺服器上傳下載檔案
首先宣告一下。jsp從ftp伺服器上面下載檔案,有兩種方法1.直接給出檔案的地址 2.將檔案作為位元組流返回給瀏覽器 一 、先說第一種吧(這種發放很簡單)。 1.直接使用一個超連結:<a href='ftp://FTP使用者名稱:密碼@IP地址:埠號+ 後面是路徑 (
CentOS6.5下搭建FTP伺服器 + Java上傳檔案
/** * @Type ftpClientFileManager.java * @Desc * @author 123 * @date 2017年3月31日 下午2:50:58 * @version */ @Service public class FTPFileManager {
java實現檔案上傳ftp伺服器功能
java檔案上傳至ftp伺服器 廢話不多說,直接上程式碼,這裡提供兩種方法,此程式執行需要下載apache-commons-net.jar包,可以使用maven倉庫下載,也可以使用http://www.java2s.com/搜尋下載。 第一種方法:使用下
Java 上傳檔案至FTP伺服器
首先,引入相關jar包 <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version&
Java Springboot結合FastDFS實現檔案上傳以及根據圖片url將圖片上傳至圖片伺服器
上一篇文章我們已經講解了如何搭建FastDFS圖片伺服器,環境我們準備好了現在就讓我們開始與Java結合將他應用到實際的專案中吧。本篇文章我們將會展示上傳圖片到FastDFS圖片伺服器以及通過外網的圖片url將圖片上傳至我們自己的圖片伺服器中。 1.建立springbo
java上傳檔案到 linux 下的 tomcat 檔案伺服器
linux 安裝 tomcat 伺服器,我安裝的是 tomcat 7,未測試其他版本 linux 安裝 tomcat 連線 https://mp.csdn.net/mdeditor/83987640# 本次使用的阿里雲 linux os7 , tomcat 7 + ssm 1、修改目
關於java上傳到ftp檔案為空的處理筆記
這幾天用java上傳圖片到ftp,上傳成功,有資料夾,有名字,就是檔案大小為0,內容空,翻遍了網上大佬們的解決辦法,終於搞定了。 如下簡單快捷 需要在ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 前面新增FTPClient.enterLo
JAVA使用JSCH實現檔案上傳到linux伺服器
1 匯入jar包 <dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version
java實現多檔案上傳至本地伺服器
博主最近在做一個內網專案,內部可以訪問外部資料,但是外部訪問不了內部資料,這也就造成了可能檔案無法上傳,所以博主另闢蹊徑,在本地伺服器上建立一個資料夾專門用來儲存上傳資料。 環境:jdk,tomcat 一、前臺上傳檔案(ajax上傳) <input type=
Java Web 學習筆記之一:伺服器獲取表單上傳的檔案
Servlet3.0標準之後,Java Web檔案上傳就不需要通過FileUpload等庫來實現了,通過servlet API即可實現web後臺的檔案上傳支援。 表單提交請求,表單中的檔案可以通過request.getPart()方法獲取。 下面來介紹獲取檔案的具體使用方
java 上傳檔案到伺服器之jquery.uploadify
基於jquery的檔案上傳控制元件,支援ajax無重新整理上傳,多個檔案同時上傳,上傳進行進度顯示,刪除已上傳檔案。 要求使用jquery1.4或以上版本,flash player 9.0.24以上。 有兩個版本,一個用flash,一個是html5。html5的需要
winscp 命令列 上傳ftp伺服器檔案
path路徑裡 配置winscp.exe 所在的路徑 winscp.exe /console /command "option batch continue" "option confirm off"
Java 實現Ftp上傳下載檔案
最近專案中需要實現將檔案先存放到ftp上,需要的時候再從ftp上下載,做的過程中碰到了問題,發現中文檔名的無法上傳到ftp上,ftp上的中文檔名的檔案下載不下來,幾番折騰,終於搞定了,記錄一下備忘。首先給工程新增maven依賴,pom.xml檔案中新增:<depende
scala和java解壓zip的檔案,並上傳到hdfs伺服器
之前一直使用的事java開發的,後來學習hadoop,spark以後,需要做一個解壓檔案到伺服器的例子,由於學習scala時間不長,故先用java程式碼寫出來,在改成scala的程式碼,過程中出現了很多問題,我將會在最後說明,請大家注意不要少了包。 環境宣告:windows