parent
682d3710ee
commit
22d87f7791
@ -0,0 +1,189 @@ |
||||
package com.gitee.sop.websiteserver.bean; |
||||
|
||||
import lombok.Data; |
||||
import okhttp3.Cookie; |
||||
import okhttp3.CookieJar; |
||||
import okhttp3.FormBody; |
||||
import okhttp3.HttpUrl; |
||||
import okhttp3.MultipartBody; |
||||
import okhttp3.OkHttpClient; |
||||
import okhttp3.Request; |
||||
import okhttp3.RequestBody; |
||||
import okhttp3.Response; |
||||
|
||||
import java.io.IOException; |
||||
import java.nio.charset.StandardCharsets; |
||||
import java.util.ArrayList; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/** |
||||
* @author tanghc |
||||
*/ |
||||
public class HttpTool { |
||||
private static final String METHOD_GET = "get"; |
||||
private Map<String, List<Cookie>> cookieStore = new HashMap<String, List<Cookie>>(); |
||||
|
||||
private OkHttpClient httpClient; |
||||
|
||||
public HttpTool() { |
||||
this(new HttpToolConfig()); |
||||
} |
||||
|
||||
public HttpTool(HttpToolConfig httpToolConfig) { |
||||
this.initHttpClient(httpToolConfig); |
||||
} |
||||
|
||||
protected void initHttpClient(HttpToolConfig httpToolConfig) { |
||||
httpClient = new OkHttpClient.Builder() |
||||
.connectTimeout(httpToolConfig.connectTimeoutSeconds, TimeUnit.SECONDS) // 设置链接超时时间,默认10秒
|
||||
.readTimeout(httpToolConfig.readTimeoutSeconds, TimeUnit.SECONDS) |
||||
.writeTimeout(httpToolConfig.writeTimeoutSeconds, TimeUnit.SECONDS) |
||||
.cookieJar(new CookieJar() { |
||||
public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) { |
||||
cookieStore.put(httpUrl.host(), list); |
||||
} |
||||
public List<Cookie> loadForRequest(HttpUrl httpUrl) { |
||||
List<Cookie> cookies = cookieStore.get(httpUrl.host()); |
||||
return cookies != null ? cookies : new ArrayList<Cookie>(); |
||||
} |
||||
}).build(); |
||||
} |
||||
|
||||
@Data |
||||
public static class HttpToolConfig { |
||||
/** 请求超时时间 */ |
||||
private int connectTimeoutSeconds = 10; |
||||
/** http读取超时时间 */ |
||||
private int readTimeoutSeconds = 10; |
||||
/** http写超时时间 */ |
||||
private int writeTimeoutSeconds = 10; |
||||
} |
||||
|
||||
/** |
||||
* get请求 |
||||
* |
||||
* @param url |
||||
* @param header |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public String get(String url, Map<String, String> header) throws IOException { |
||||
Request.Builder builder = new Request.Builder().url(url).get(); |
||||
// 添加header
|
||||
addHeader(builder, header); |
||||
|
||||
Request request = builder.build(); |
||||
Response response = httpClient.newCall(request).execute(); |
||||
return response.body().string(); |
||||
} |
||||
|
||||
/** |
||||
* 提交表单 |
||||
* |
||||
* @param url url |
||||
* @param form 参数 |
||||
* @param header header |
||||
* @param method 请求方式,post,get等 |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public String request(String url, Map<String, String> form, Map<String, String> header, String method) throws IOException { |
||||
Request.Builder requestBuilder; |
||||
if (METHOD_GET.equalsIgnoreCase(method)) { |
||||
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder(); |
||||
for (Map.Entry<String, String> entry : form.entrySet()) { |
||||
urlBuilder.addQueryParameter(entry.getKey(), entry.getValue()); |
||||
} |
||||
requestBuilder = new Request.Builder() |
||||
.url(urlBuilder.build()) |
||||
.get(); |
||||
} else { |
||||
FormBody.Builder paramBuilder = new FormBody.Builder(StandardCharsets.UTF_8); |
||||
for (Map.Entry<String, String> entry : form.entrySet()) { |
||||
paramBuilder.add(entry.getKey(), entry.getValue()); |
||||
} |
||||
FormBody formBody = paramBuilder.build(); |
||||
requestBuilder = new Request.Builder() |
||||
.url(url) |
||||
.method(method, formBody); |
||||
} |
||||
// 添加header
|
||||
addHeader(requestBuilder, header); |
||||
|
||||
Request request = requestBuilder.build(); |
||||
Response response = httpClient |
||||
.newCall(request) |
||||
.execute(); |
||||
try { |
||||
return response.body().string(); |
||||
} finally { |
||||
response.close(); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 提交表单,并且上传文件 |
||||
* |
||||
* @param url |
||||
* @param form |
||||
* @param header |
||||
* @param files |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public String requestFile(String url, Map<String, String> form, Map<String, String> header, List<UploadFile> files) |
||||
throws IOException { |
||||
// 创建MultipartBody.Builder,用于添加请求的数据
|
||||
MultipartBody.Builder bodyBuilder = new MultipartBody.Builder(); |
||||
bodyBuilder.setType(MultipartBody.FORM); |
||||
|
||||
for (UploadFile uploadFile : files) { |
||||
bodyBuilder.addFormDataPart(uploadFile.getName(), // 请求的名字
|
||||
uploadFile.getFileName(), // 文件的文字,服务器端用来解析的
|
||||
RequestBody.create(null, uploadFile.getFileData()) // 创建RequestBody,把上传的文件放入
|
||||
); |
||||
} |
||||
|
||||
Set<Map.Entry<String, String>> entrySet = form.entrySet(); |
||||
for (Map.Entry<String, String> entry : entrySet) { |
||||
bodyBuilder.addFormDataPart(entry.getKey(), entry.getValue()); |
||||
} |
||||
|
||||
RequestBody requestBody = bodyBuilder.build(); |
||||
|
||||
Request.Builder builder = new Request.Builder().url(url).post(requestBody); |
||||
|
||||
// 添加header
|
||||
addHeader(builder, header); |
||||
|
||||
Request request = builder.build(); |
||||
Response response = httpClient.newCall(request).execute(); |
||||
try { |
||||
return response.body().string(); |
||||
} finally { |
||||
response.close(); |
||||
} |
||||
} |
||||
|
||||
private void addHeader(Request.Builder builder, Map<String, String> header) { |
||||
if (header != null) { |
||||
Set<Map.Entry<String, String>> entrySet = header.entrySet(); |
||||
for (Map.Entry<String, String> entry : entrySet) { |
||||
builder.addHeader(entry.getKey(), String.valueOf(entry.getValue())); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public void setCookieStore(Map<String, List<Cookie>> cookieStore) { |
||||
this.cookieStore = cookieStore; |
||||
} |
||||
|
||||
public void setHttpClient(OkHttpClient httpClient) { |
||||
this.httpClient = httpClient; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,60 @@ |
||||
package com.gitee.sop.websiteserver.bean; |
||||
|
||||
|
||||
import com.gitee.sop.websiteserver.util.FileUtil; |
||||
import lombok.Getter; |
||||
import lombok.Setter; |
||||
import org.apache.commons.codec.digest.DigestUtils; |
||||
|
||||
import java.io.File; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* 文件上传类 |
||||
* @author tanghc |
||||
*/ |
||||
@Getter |
||||
@Setter |
||||
public class UploadFile implements Serializable { |
||||
private static final long serialVersionUID = -1100614660944996398L; |
||||
|
||||
/** |
||||
* @param name 表单名称,不能重复 |
||||
* @param file 文件 |
||||
* @throws IOException |
||||
*/ |
||||
public UploadFile(String name, File file) throws IOException { |
||||
this(name, file.getName(), FileUtil.toBytes(file)); |
||||
} |
||||
|
||||
/** |
||||
* @param name 表单名称,不能重复 |
||||
* @param fileName 文件名 |
||||
* @param input 文件流 |
||||
* @throws IOException |
||||
*/ |
||||
public UploadFile(String name, String fileName, InputStream input) throws IOException { |
||||
this(name, fileName, FileUtil.toBytes(input)); |
||||
} |
||||
|
||||
/** |
||||
* @param name 表单名称,不能重复 |
||||
* @param fileName 文件名 |
||||
* @param fileData 文件数据 |
||||
*/ |
||||
public UploadFile(String name, String fileName, byte[] fileData) { |
||||
super(); |
||||
this.name = name; |
||||
this.fileName = fileName; |
||||
this.fileData = fileData; |
||||
this.md5 = DigestUtils.md5Hex(fileData); |
||||
} |
||||
|
||||
private String name; |
||||
private String fileName; |
||||
private byte[] fileData; |
||||
private String md5; |
||||
|
||||
} |
@ -0,0 +1,65 @@ |
||||
package com.gitee.sop.websiteserver.util; |
||||
|
||||
import java.io.ByteArrayOutputStream; |
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.FileNotFoundException; |
||||
import java.io.IOException; |
||||
import java.io.InputStream; |
||||
|
||||
public class FileUtil { |
||||
|
||||
/** |
||||
* The default buffer size to use. |
||||
*/ |
||||
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; |
||||
private static final int EOF = -1; |
||||
|
||||
/** |
||||
* 将文件流转换成byte[] |
||||
* @param input |
||||
* @return |
||||
* @throws IOException |
||||
*/ |
||||
public static byte[] toBytes(InputStream input) throws IOException { |
||||
ByteArrayOutputStream output = new ByteArrayOutputStream(); |
||||
int n = 0; |
||||
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; |
||||
|
||||
while (EOF != (n = input.read(buffer))) { |
||||
output.write(buffer, 0, n); |
||||
} |
||||
return output.toByteArray(); |
||||
} |
||||
|
||||
/** |
||||
* 将文件转换成数据流 |
||||
* @param file 文件 |
||||
* @return 返回数据流 |
||||
* @throws IOException |
||||
*/ |
||||
public static byte[] toBytes(File file) throws IOException { |
||||
if (file.exists()) { |
||||
if (file.isDirectory()) { |
||||
throw new IOException("File '" + file + "' exists but is a directory"); |
||||
} |
||||
if (file.canRead() == false) { |
||||
throw new IOException("File '" + file + "' cannot be read"); |
||||
} |
||||
} else { |
||||
throw new FileNotFoundException("File '" + file + "' does not exist"); |
||||
} |
||||
InputStream input = null; |
||||
try { |
||||
input = new FileInputStream(file); |
||||
return toBytes(input); |
||||
} finally { |
||||
try { |
||||
if (input != null) { |
||||
input.close(); |
||||
} |
||||
} catch (IOException ioe) { |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,38 @@ |
||||
package com.gitee.sop.websiteserver.util; |
||||
|
||||
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload; |
||||
import org.springframework.web.multipart.MultipartFile; |
||||
import org.springframework.web.multipart.MultipartHttpServletRequest; |
||||
|
||||
import javax.servlet.http.HttpServletRequest; |
||||
import java.util.Collection; |
||||
import java.util.Collections; |
||||
import java.util.Map; |
||||
import java.util.Optional; |
||||
|
||||
/** |
||||
* 文件上传工具类 |
||||
* |
||||
* @author tanghc |
||||
*/ |
||||
public class UploadUtil { |
||||
|
||||
/** |
||||
* 获取上传文件 |
||||
* |
||||
* @param request |
||||
* @return |
||||
*/ |
||||
public static Collection<MultipartFile> getUploadFiles(HttpServletRequest request) { |
||||
Map<String, MultipartFile> fileMap = null; |
||||
//检查form中是否有enctype="multipart/form-data"
|
||||
if (ServletFileUpload.isMultipartContent(request)) { |
||||
//将request变成多部分request
|
||||
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request; |
||||
fileMap = multiRequest.getFileMap(); |
||||
} |
||||
return Optional.ofNullable(fileMap) |
||||
.map(map -> map.values()) |
||||
.orElse(Collections.emptyList()); |
||||
} |
||||
} |
Loading…
Reference in new issue