Java 图片爬虫,java打包jar文件
1. Java 图片爬虫,制作 .jar 文件
spider.java
spider.java
高清图片api : https://www.xwboke.cn/api/api.php ,每请求一次,会更换一张高清大图。
API1:
API2
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class spider{
public static void main(String[] args) throws Exception{
long start = System.currentTimeMillis();
System.out.print("start:");
System.out.println(start);
for(int i=1;i<=5;i++){
String url="https://www.xwboke.cn/api/api.php";
getImg(url,i);
System.out.println("Finished"+i);
}
long end=System.currentTimeMillis();
System.out.println("run time: "+(end-start)/1000+" s");
}
private static void getImg(String u, int i){
URL url;
try{
url = new URL(u);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5*1000);
InputStream in = conn.getInputStream();
byte[] data = readInputStream(in);
File f = new File(i+".jpg");
FileOutputStream out = new FileOutputStream(f);
out.write(data);
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
private static byte[] readInputStream(InputStream ins) throws IOException{
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = ins.read(buffer)) != -1){
out.write(buffer, 0, len);
}
ins.close();
return out.toByteArray();
}
}
编译: javac spider.java
执行:java spider
root@kali:~/Desktop# java spider
start:1536475728353
Finished1
Finished2
Finished3
Finished4
Finished5
run time: 19 s
root@kali:~/Desktop#
制作 jar 文件
jar cvf 5imgGet.jar spider.class
此时直接运行:java -jar 5imgGet.jar
会报错:

直接将jar文件解压开,查看其中的 META-INF/MANIFEST.MF 文件,
root@kali:~/Desktop/tmp# cat META-INF/MANIFEST.MF
Manifest-Version: 1.0
Created-By: 1.8.0_151 (Oracle Corporation)
root@kali:~/Desktop/tmp#
只有两行,在第三行添加 Main-Class: spider,指定主类

保存后执行 :jar umf MANIFEST.MF 5imgGet.jar

这样,这个jar文件就可以执行了。


添加执行权限

reference : https://www.cnblogs.com/liang-io/p/9338184.html
Java 图片爬虫,java打包jar文件的更多相关文章
- Intellij打包jar文件,“java.lang.SecurityException: Invalid signature file digest for Manifest main attrib
下面是使用Intellij 打包jar文件的步骤,之后会有运行jar文件时遇到的错误. 打包完成. ================================================== ...
- Eclipse将android项目打包jar文件
Eclipse+android打包jar文件 蔡建良 2016-3-12 以Android-SlideExpandableListView开源框架为例,将源码Library打包成jar文件并包含R.c ...
- 关于在打包Jar文件时遇到的资源路径问题(二)
在关于<关于在打包Jar文件时遇到的资源路径问题(一)>中,以及描述了当资源与可执行JAr分离时的资源路径代码的编写问题,后来想了想,为什么将<Java核心技术卷一>中的程序1 ...
- 关于在打包Jar文件时遇到的资源路径问题(一)
当我们将程序写好,并进行打包成Jar文件时,通常都带有各种资源,这些资源可以是图像或者声音文件,也可以是别的如文本文件或二进制文件等,这些资源都和代码密切相关.例如在一个JPanel类上显示一些可能变 ...
- AndroidStduio3.0 使用gradle将module打包jar文件
AndroidStduio3.0使用gradle将module打包jar文件,首先需要安装gradle. 打开控制台输入 open -e .bash_profile 命令,就可以打开 ...
- Java中基本的打包jar和war文件(包括eclipse打包操作)
前言: 为什么要打jar包? 1.举个例子,当编写一个工具类库,里面有十几个类,每个类对应一个class文件时,为了方便别人调用,是不是要装在一个文件中,方便传递和引用. 2.打jar包还有一个好处, ...
- java 打包jar文件以在没有安装JDK或JRE的机子上运行
前言: java号称“一次编译,到处运行”,但这有个前提,那就是你的机子上得安装java环境.对于开发人员或其他一些比较懂计算机的人来说这没什么,但是对于一些不懂计算机的人来说这会很麻烦,他们更希望的 ...
- Java GUI使用exe4j打包exe文件
exe4j下载地址:http://blog.csdn.net/cciii/article/details/17083531 1. 在MyEclipse将java项目打包成可执行jar文件.项目结构如 ...
- [转载] Java中动态加载jar文件和class文件
转载自http://blog.csdn.net/mousebaby808/article/details/31788325 概述 诸如tomcat这样的服务器,在启动的时候会加载应用程序中lib目录下 ...
随机推荐
- [Swift]LeetCode354. 俄罗斯套娃信封问题 | Russian Doll Envelopes
You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envel ...
- [Swift]LeetCode879. 盈利计划 | Profitable Schemes
There are G people in a gang, and a list of various crimes they could commit. The i-th crime generat ...
- Qt创建分割窗口
1.QT中QSplitter类可以用来灵活分割窗口,从而产生可用的布局,在以后进行界面布局很有用. 2.先看代码,这个分割窗口按顺序添加子窗口: #include "mainwindow.h ...
- SpringBoot前后端分离Instant时间戳自定义解析
在SpringBoot项目中,前后端规定传递时间使用时间戳(精度ms). @Data public class Incident { @ApiModelProperty(value = "故 ...
- BBS论坛(四)
4.1.cms登录页面csrf保护 (1)Perfect_bbs.py from flask_wtf import CSRFProtect CSRFProtect(app) 添加csrf保护后,现在再 ...
- socket编程: TypeError: must be bytes or buffer, not str
先看一段代码 #!/usr/bin/env python3 from socket import * serverName = "10.10.10.132" serverPort ...
- Ocelot统一权限验证
Ocelot作为网关,可以用来作统一验证,接上一篇博客,我们继续 前一篇,我们创建了OcelotGateway网关项目,DemoAAPI项目,DemoBAPI项目,为了验证用户并分发Token,现在还 ...
- redis 系列25 哨兵Sentinel (高可用演示 下)
一. Sentinel 高可用环境准备 1.1 Sentinel 集群环境 环境 说明 操作系统版本 CentOS 7.4.1708 IP地址 172.168.18.200 网关Gateway 1 ...
- 理解和使用Promise.all和Promise.race
一.Pomise.all的使用 Promise.all可以将多个Promise实例包装成一个新的Promise实例.同时,成功和失败的返回值是不同的,成功的时候返回的是一个结果数组,而失败的时候则返回 ...
- ES6 系列之 Babel 是如何编译 Class 的(下)
前言 ES5 寄生组合式继承 function Parent (name) { this.name = name; } Parent.prototype.getName = function () { ...