用jsoup 采集图片:
public void jsoupHTMLImg(Document doc) throws Exception{
Element imgUl = doc.getElementById("image");
Elements srcLinks = imgUl.select("a[href$=.jpg]");
for(Element link : srcLinks) {
//:剔除标签,只剩链接路径
String imagesPath = link.attr("href");
String ext = FilenameUtils.getExtension(imagesPath);
if(!imagesPath.contains("http")){
imagesPath = "http://ccccc.com"+imagesPath;
}
System.out.println("当前访问路径:"+imagesPath);
getImages(imagesPath, UploadUtils.UPLOAD_DIR);
}
System.out.println("下载完成!----类型1");
}
/**
* @param urlPath 图片路径
* @throws Exception
*/
public void getImages(String urlPath, String filePath, String fileName) {
try{
URL url = new URL(urlPath);//:获取的路径
//:http协议连接对象
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(6 * 10000);
if (conn.getResponseCode() == 200){
InputStream inputStream = conn.getInputStream();
byte[] data = readStream(inputStream);
if(data.length>(1024*10)){
FileOutputStream outputStream = new FileOutputStream(filePath + fileName);
outputStream.write(data);
outputStream.close();
}
}
} catch (Exception e){
e.printStackTrace();
System.out.println("下载图片出错"+e.getMessage() + " " + e.getCause());
}
}
/**
* 读取url中数据,并以字节的形式返回
* @param inputStream
* @return
* @throws Exception
*/
public byte[] readStream(InputStream inputStream) throws Exception{
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[2048];
int len = -1;
while((len = inputStream.read(buffer)) !=-1){
outputStream.write(buffer, 0, len);
}
outputStream.close();
inputStream.close();
return outputStream.toByteArray();
}
FTP上传:
/**
* Description: 向FTP服务器上传文件
* @Version1.0 Jul 27, 2008 4:31:09 PM by 崔红保(cuihongbao@d-heaven.com)创建
* @param url FTP服务器hostname
* @param port FTP服务器端口
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);//连接FTP服务器
//如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);//登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
@org.junit.Test
public void testUpLoadFromDisk(){
try {
FileInputStream in=new FileInputStream(new File("E://apache-tomcat-7.0.40-oa/webapps/file/images/141638628076812208708851290812972.jpg"));
boolean flag = uploadFile("121.XXXXX", 21, "XX", "XXXX$XXXXX", "/home", "test.txt", in);
System.out.println(flag);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
SFTP:
/*
* Created on 2009-9-14
* Copyright 2009 by www.xfok.net. All Rights Reserved
*
*/
package net.xfok.ftp;
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;
/**
* @author YangHua
* 转载请注明出处:http://www.xfok.net/2009/10/124485.html
*/
public class MySFTP {
/**
* 连接sftp服务器
* @param host 主机
* @param port 端口
* @param username 用户名
* @param password 密码
* @return
*/
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);
System.out.println("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
System.out.println("Session connected.");
System.out.println("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
System.out.println("Connected to " + host + ".");
} catch (Exception e) {
}
return sftp;
}
/**
* 上传文件
* @param directory 上传的目录
* @param uploadFile 要上传的文件
* @param sftp
*/
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();
}
}
/**
* 下载文件
* @param directory 下载目录
* @param downloadFile 下载的文件
* @param saveFile 存在本地的路径
* @param sftp
*/
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();
}
}
/**
* 删除文件
* @param directory 要删除文件所在目录
* @param deleteFile 要删除的文件
* @param sftp
*/
public void delete(String directory, String deleteFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 列出目录下的文件
* @param directory 要列出的目录
* @param sftp
* @return
* @throws SftpException
*/
public Vector listFiles(String directory, ChannelSftp sftp) throws SftpException{
return sftp.ls(directory);
}
public static void main(String[] args) {
MySFTP sf = new MySFTP();
String host = "192.168.0.1";
int port = 22;
String username = "root";
String password = "root";
String directory = "/home/httpd/test/";
String uploadFile = "D:\\tmp\\upload.txt";
String downloadFile = "upload.txt";
String saveFile = "D:\\tmp\\download.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{
sftp.cd(directory);
sftp.mkdir("ss");
System.out.println("finished");
}catch(Exception e){
e.printStackTrace();
}
}
}
maven:
http://search.maven.org/
版本:
<
dependency
>
<
groupId
>com.jcraft</
groupId
>
<
artifactId
>jsch</
artifactId
>
<
version
>0.1.51</
version
>
</
dependency
>