最近做一个增加产品的接口,需要上传图片到前台路径下就通过前台FTPClient实现后台将文件转存到前台服务器路径下,
public String hostname = "hxxxxxxxxxxx"; //ftp服务器端口号默认为21 public Integer port = 21 ; //ftp登录账号 public String username = "xxxxxxx"; //ftp登录密码 public String password = "xxxxxxx"; public FTPClient ftpClient = null; /** * 初始化ftp服务器 */ public void initFtpClient() { ftpClient = new FTPClient(); ftpClient.setControlEncoding("utf-8"); try { System.out.println("connecting...ftp服务器:"+this.hostname+":"+this.port); ftpClient.connect(hostname, port); //连接ftp服务器 ftpClient.login(username, password); //登录ftp服务器 int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器 if(!FTPReply.isPositiveCompletion(replyCode)){ System.out.println("connect failed...ftp服务器:"+this.hostname+":"+this.port); } System.out.println("connect successfu...ftp服务器:"+this.hostname+":"+this.port); }catch (MalformedURLException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } }
public boolean uploadFile( String pathname, String fileName,InputStream inputStream){ boolean flag = false; try{ System.out.println("开始上传文件"); initFtpClient(); 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{ if(ftpClient.isConnected()){ try{ ftpClient.disconnect(); }catch(IOException e){ e.printStackTrace(); } } if(null != inputStream){ try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return true; }
controlls层实现:
@ApiOperation(value = "获取产品列表", notes = "获取产品列表") @GetMapping("/selectProduct") public List<Product> selectProduct() { return productService.selectInfo(); } @ApiOperation(value = "新增产品", notes = "新增产品") @PostMapping("/InsertProduct") public String InsertProduct(@RequestParam MultipartFile file, @RequestParam String ProductName, @RequestParam String ProductInfo) { try { File dir = new File("upload/"); if(!dir.exists()) { dir.mkdir(); } String path = "upload/"+ file.getOriginalFilename(); // File tempFile = null;tempFile = new File(path); //file.transferTo(tempFile); FileUtil.uploadFile(file.getBytes(), "upload/", file.getOriginalFilename()); String ArmfilePath = "/htdocs/images"; FTPUtil obj = new FTPUtil(); obj.uploadFile(ArmfilePath,file.getOriginalFilename(),new FileInputStream(path)); File deletefile = new File(path); deletefile.delete(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } Product obj = new Product(); obj.setName(ProductName); obj.setInfo(ProductInfo); obj.setFilepath("images/" + file.getOriginalFilename()); productService.insertInfo(obj); return "OK"; }
先上传到临时文件,再将文件上传到FTP路径下。
public class FileUtil { public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception { File targetFile = new File(filePath); if(!targetFile.exists()){ targetFile.mkdirs(); } FileOutputStream out = new FileOutputStream(filePath+fileName); out.write(file); out.flush(); out.close(); } }