jsoup简单学习
void upFile() throws IOException {
String filePath = "D:/tmp/1.jpg";
String filePath2 = "D:/tmp/2.jpg";
FileInputStream inputStream = FileUtils.openInputStream(new File(filePath));
FileInputStream inputStream2 = FileUtils.openInputStream(new File(filePath2));
Connection.Response response =
Jsoup.connect("http://localhost:9590/thy/tools/upfile")
.userAgent("Mozilla/5.0")
.timeout(10 * 1000)
.method(Connection.Method.POST)
// 字段参数| 文件名称| 文件流
.data("fs", FilenameUtils.getName(filePath), inputStream)
.data("fs2", FilenameUtils.getName(filePath2), inputStream2)
.data("destination", "/welcome")
.followRedirects(true)
.execute();
String body = response.body();
System.out.println(body);
}
上传文件部分是使用spring mvc 可以在不知道表单字段的情况下接受文件
@ApiOperation("在不知道文件名情况下获取上传文件")
@ApiImplicitParam(name = "is", value = "上传图片a", allowMultiple = true, dataType = "file")
@RequestMapping(value = "upfile", method = RequestMethod.POST)
public @ResponseBody
String uploadOssFile(MultipartFile file, HttpServletRequest request, String is) {
String picPath = environment.getProperty("sys.config.pic.savepath");
String picUrl = environment.getProperty("sys.config.pic.baseurl");
log.info(StringUtils.repeat('-', 28));
// String url = ossService.uploadOssFile(file);
MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
Map<String, MultipartFile> fileMap = mr.getFileMap();
Set<Map.Entry<String, MultipartFile>> entries = fileMap.entrySet();
String url = null;
for (Map.Entry<String, MultipartFile> entry : entries) {
MultipartFile multipartFile = entry.getValue();
String key = entry.getKey();
url = multipartFile.getOriginalFilename();
log.info("key-value|表单字段和文件名分别是|:{},{}", key, multipartFile.getOriginalFilename());
try {
// TODO 此方法无法自动创建不存在的文件夹
// multipartFile.transferTo(new File(picPath +
// multipartFile.getOriginalFilename()));
// 文件保存
url = com.study.utils.SysUtils.getDateStrSpan_() + "/" + com.study.utils.SysUtils.getDate4() + multipartFile.getOriginalFilename();
// TODO 可以自动创建不存在的文件夹
FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(picPath + url));
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return String.format("%s/%s", picUrl, url);
}
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import java.io.IOException;
import java.util.Map;
/**
* @创建时间 2020/5/7 14:14
*/
public class SignIn {
private static final String USER_NAME = " ";
private static final String USER_PWD = " ";
private static final String REFERRER_URL = "http://******/";
private static final String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.34 Safari/537.36 Edg/83.0.478.25";
private static final String HOST = "******";
//登录URL
private static String URL_LOGIN = "http://******/*&/";
//签到 URL
private static final String URL_Sign_in = "http://******/e/dongpo";
//领取全勤 奖励
private static final String URL_Sign_ALL = "http://******/e";
//签到成功后的cookie
private static Map<String, String> cookies;
private static Connection connection;
static {
System.out.println("---------签到------------");
connection = Jsoup.connect(URL_LOGIN).userAgent(userAgent)
.timeout(10 * 1000)
.method(Connection.Method.POST)
.data("username", USER_NAME).data("password", USER_PWD)
.data("enews", "login").data("ecmsfrom", "").data("lifetime", "0")
// // 字段参数| 文件名称| 文件流
.header("Connection", "keep-alive")
.followRedirects(true);
connection.header("Host", HOST)
.header("Referer", REFERRER_URL)
.header("Origin", REFERRER_URL);
//Host: ******
//Origin: http://******
Connection.Response execute = null;
try {
execute = connection.ignoreContentType(true).execute();
System.out.println(execute.body());
} catch (IOException e) {
e.printStackTrace();
}
cookies = execute.cookies();
System.out.println("登录cookie");
System.out.println(cookies);
}
/**
* jsoup 模拟签到
*
* @param args
*/
public static void main(String[] args) throws IOException {
signForScore(connection, cookies, "1");
signForScore(connection, cookies, "2");
}
/**
* 签到领取积分
*
* @param connection
* @param cookies
* @param type 1 签到 2 领取全勤
* @throws IOException
*/
private static void signForScore(Connection connection, Map<String, String> cookies, String type) throws IOException {
Connection.Response response = connection.url(URL_Sign_in).method(Connection.Method.POST)
.data("enews", type)
.cookies(cookies)
.execute();
System.out.println(response.body());
System.out.println("------------");
}
}