1.dao层实现:
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import spider.utils.JdbcUtil;
import javax.sql.DataSource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class SelectAndCopyImpl implements ISelectAndCopy{
@Override
public void selectAndCopy(String href) {
Document document = null;
try {
//根据网址获得网页源代码文档对象
document = Jsoup.connect(href).get();
//选择文档对象中所有的 带有src的img图片标签
Elements select = document.select("img[src]");
for (Element element : select) {
String src=element.attr("src");
System.out.println(src);
try {
String theSrc="http:"+src;
InputStream inputStream = new URL(theSrc).openStream();
FileOutputStream fileOutputStream = new FileOutputStream("D:\\img/" + System.currentTimeMillis() + ".jpg");
IOUtils.copy(inputStream,fileOutputStream);
DataSource ds = JdbcUtil.getDs();
System.out.println(ds);
// 创建File对象
File destFile = new File("D:\\img/");
// 获取所有文件和文件夹
File[] files = destFile.listFiles();
// 遍历
for(File file : files) {
String imgName = file.getName();
try {
System.out.println("图片名"+imgName+"路径"+theSrc);
PreparedStatement preparedStatement = ds.getConnection().prepareStatement("insert into img(imgName,src) values (?,?)");
preparedStatement.setString(1,imgName);
preparedStatement.setString(2,theSrc);
int i = preparedStatement.executeUpdate();
//System.out.println("受影响的行数为:"+i);
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.Servlet层:
@WebServlet("/spider")
public class spiderServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String href = request.getParameter("href");
new SelectAndCopyServiceImpl().selsctAndCopy(href);
}
}