如果你想做一个带有验证码的登陆系统然后存储cookie,来进行爬取数据操作(例如登陆某教务系统做一个课表小程序),这里展示验证码的前后端交互
后端代码
- 获取验证码(使用流的方式)
@GetMapping("/getVerifyImg")
public void getVerifyImg3(HttpServletResponse response2, String userId) {
Connection conn = Jsoup.connect("验证码请求网址").ignoreContentType(true);
Map<String, String> header = new HashMap<String, String>();
header.put("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0)" +
" Gecko/20100101 Firefox/5.0");
conn.headers(header);
Connection.Response response;
try {
response = conn.execute();
Map<String, String> cookies = response.cookies();
BufferedInputStream bufferedInputStream = response.bodyStream();
byte[] b = new byte[1024];
int i = bufferedInputStream.read(b);
OutputStream outputStream = new BufferedOutputStream(response2.getOutputStream());
try {
while (i != -1) {
outputStream.write(b, 0, i);
outputStream.flush();
i = bufferedInputStream.read(b);
}
outputStream.flush();
bufferedInputStream.close();
} catch (SocketException se) {
System.out.println("fail");
}
} catch (IOException e) {
System.out.println("fail");
}
}
- 使用示例
<image src="https://xxx/xx/getVerifyImg?userId=2"></image>
- 获取验证码(使用文件保存方式)
思路:把输入流保存为图片,然后返回图片的服务器地址
保存为图片的主要代码:
byte[] b = new byte[1024];
int i = bufferedInputStream.read(b);
//保存文件
FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\seldo\\Desktop\\banner\\code.jpg"));
try {
while (i != -1) {
outputStream.write(b, 0, i);
outputStream.flush();
i = bufferedInputStream.read(b);
}
outputStream.flush();
bufferedInputStream.close();
} catch (SocketException se) {
System.out.println("fail");
}
pom文件,要自己选择自己需要的包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.29</version>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.6.1</version>
</dependency>
<!--websocket-->
<!--核心依赖项-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<!--<version>1.8.3</version>-->
<version>1.11.3</version>
</dependency>
<dependency>
<groupId>com.asprise.ocr</groupId>
<artifactId>java-ocr-api</artifactId>
<version>[15,)</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>4.1.1</version>
<exclusions>
<exclusion>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.2.1</version>
</dependency>
</dependencies>