将图片上传到另一台服务器处理
2016-04-05 22:15
471 查看
最近做项目要将图片上传到另一台服务器,查资料实现了三种方法,下面就介绍一下方法。
需要的jar包:jsch-0.1.42-sources.jar jsch-0.1.42.jar
还可参考:JSch - Java实现的SFTP(文件上传详解篇)
Linux平台下快速搭建FTP服务器
需要的jar包:commons-io-1.4-javadoc.jar commons-io-1.4-sources.jar commons-io-1.4.jar
想配置的话可参考http://www.tuicool.com/wx/aUrAzm
下载的东西最好按照文章中来,我起初用的nginx1.9.0怎么配置都不行,坑死个人 本人配置了一下午,有兴趣的可以实现一下
1.sftp(最简单)
package com.xiang; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.Properties; import java.util.Vector; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; import com.jcraft.jsch.SftpException; public class Main { /*host 主机 port 端口 username 用户名 password 密码*/ public ChannelSftp connect(String host, int port, String username, String password) { ChannelSftp sftp = null; try { JSch jsch = new JSch(); jsch.getSession(username, host, port); Session sshSession = jsch.getSession(username, host, port); sshSession.setPassword(password); Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); sshSession.setConfig(sshConfig); sshSession.connect(); Channel channel = sshSession.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; } catch (Exception e) { } return sftp; } /*directory 上传的目录 uploadFile 要上传的文件*/ public void upload(String directory, String uploadFile, ChannelSftp sftp) { try { sftp.cd(directory); File file = new File(uploadFile); sftp.put(new FileInputStream(file), file.getName()); } catch (Exception e) { e.printStackTrace(); } } /*downloadFile 下载的文件 saveFile 存在本地的路径*/ public void download(String directory, String downloadFile, String saveFile, ChannelSftp sftp) { try { sftp.cd(directory); File file = new File(saveFile); sftp.get(downloadFile, new FileOutputStream(file)); } catch (Exception e) { e.printStackTrace(); } } /*directory 要删除文件所在目录 deleteFile 要删除的文件*/ public void delete(String directory, String deleteFile, ChannelSftp sftp) { try { sftp.cd(directory); sftp.rm(deleteFile); } catch (Exception e) { e.printStackTrace(); } } //directory 要列出的目录 public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException { return sftp.ls(directory); } public static void main(String[] args) { Main sf = new Main(); String host = "192.168.17.128"; int port = 22; String username = "root"; String password = "mgxmgx"; String directory = "/nginx/html/"; String uploadFile = "D:\\bg.jpg"; //String downloadFile = "upload.txt"; //String saveFile = "D:\\tmp\\save.txt"; //String deleteFile = "delete.txt"; ChannelSftp sftp = sf.connect(host, port, username, password); sf.upload(directory, uploadFile, sftp); //sf.download(directory, downloadFile, saveFile, sftp); //sf.delete(directory, deleteFile, sftp); try { System.out.println("成功"); } catch (Exception e) { e.printStackTrace(); } } }
需要的jar包:jsch-0.1.42-sources.jar jsch-0.1.42.jar
还可参考:JSch - Java实现的SFTP(文件上传详解篇)
2.ftp(中等)
需要搭建ftp服务Linux平台下快速搭建FTP服务器
package com.xiang; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; public class Test { public static void main(String[] args) { FTPClient ftpClient = new FTPClient(); ftpClient.setControlEncoding("GBK"); FileInputStream fis = null; try { ftpClient.connect("192.168.17.128"); ftpClient.login("test", "123456"); int reply = ftpClient.getReplyCode(); // 以2开头的返回值就会为真 if (!FTPReply.isPositiveCompletion(reply)) { ftpClient.disconnect(); System.out.println("连接服务器失败"); return; } System.out.println("登陆服务器成功"); File srcFile = new File("D:\\bg.jpg"); fis = new FileInputStream(srcFile); ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE); ftpClient.makeDirectory("123"); ftpClient.changeWorkingDirectory("123"); ftpClient.storeFile("new.jpg", fis); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("FTP客户端出错!", e); } finally { IOUtils.closeQuietly(fis); try { fis.close(); } catch (IOException e1) { e1.printStackTrace(); } try { ftpClient.disconnect(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("关闭FTP连接发生异常!", e); } } } }
需要的jar包:commons-io-1.4-javadoc.jar commons-io-1.4-sources.jar commons-io-1.4.jar
3.通过nginx上传模块上传至另一服务器nginx
相当坑,n年前的东西了,配置相当难虽然配置成功了,但感觉不好用想配置的话可参考http://www.tuicool.com/wx/aUrAzm
下载的东西最好按照文章中来,我起初用的nginx1.9.0怎么配置都不行,坑死个人 本人配置了一下午,有兴趣的可以实现一下
相关文章推荐
- AtomicInteger源码注释
- 联通子数组最大值设计03
- 软件工程学习进度条
- 【使用CMake组织C++工程】0:CMake学习资源
- 抽象模板模式
- 事件委托
- chmod 和 chown 命令用法
- 线程同步方法、性能比较(synchronized,ReentrantLock,Atomic)
- C语言实现动态二维数组
- 转发与重定向的区别
- 【BZOJ3032】七夕祭【绝对值不等式】【中位数】【数形结合】
- 如何在Ubuntu中自定义键盘快捷[译]
- Mod of power 2 on bitwise operators?
- 【codevs 1690】题解
- linux修改TCP连接数(centos实测)
- 无法远程连接mysql服务器
- nodejs 框架安装生成app
- Linux Mint (官方应用软件社区中心)
- Android 6.0 使用 HttpClient
- Java 参数检查