本文主要是介绍将图片上传到另一台服务器处理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
最近做项目要将图片上传到另一台服务器,查资料实现了三种方法,下面就介绍一下方法。
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怎么配置都不行,坑死个人 本人配置了一下午,有兴趣的可以实现一下
这篇关于将图片上传到另一台服务器处理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!