add CommonRequest, FileRequest, GsonRequest, TimiRequest
This commit is contained in:
61
src/main/java/com/imyeyu/network/FileRequest.java
Normal file
61
src/main/java/com/imyeyu/network/FileRequest.java
Normal file
@@ -0,0 +1,61 @@
|
||||
package com.imyeyu.network;
|
||||
|
||||
import com.imyeyu.io.IO;
|
||||
import org.apache.hc.client5.http.fluent.Request;
|
||||
import org.apache.hc.core5.http.Header;
|
||||
import org.apache.hc.core5.http.HttpResponse;
|
||||
import org.apache.hc.core5.http.ProtocolException;
|
||||
|
||||
import javax.naming.NoPermissionException;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2025-06-26 13:03
|
||||
*/
|
||||
public class FileRequest extends CommonRequest {
|
||||
|
||||
protected FileRequest(Request request) {
|
||||
super(request);
|
||||
}
|
||||
|
||||
public static FileRequest wrap(Request request) {
|
||||
return new FileRequest(request);
|
||||
}
|
||||
|
||||
public static FileRequest get(String url) {
|
||||
return new FileRequest(Request.get(url));
|
||||
}
|
||||
|
||||
public static FileRequest post(String url) {
|
||||
return new FileRequest(Request.post(url));
|
||||
}
|
||||
|
||||
public void toFile(String path, String fileName) throws IOException, NoPermissionException {
|
||||
toFile(new File(IO.fitPath(path) + fileName));
|
||||
}
|
||||
|
||||
public void toFile(Path outputPath) throws IOException, NoPermissionException {
|
||||
toFile(outputPath.toFile());
|
||||
}
|
||||
|
||||
public void toFile(File file) throws IOException, NoPermissionException {
|
||||
IO.toFile(file, asBytes());
|
||||
}
|
||||
|
||||
public long length() throws IOException, ProtocolException {
|
||||
HttpResponse response = request.execute().returnResponse();
|
||||
final int status = response.getCode();
|
||||
if (status < 200 || 300 <= status) {
|
||||
throw new IOException("HTTP error status: " + status);
|
||||
}
|
||||
final Header contentLengthHeader = response.getHeader("Content-Length");
|
||||
if (contentLengthHeader == null) {
|
||||
throw new IOException("Content-Length header missing");
|
||||
}
|
||||
return Long.parseLong(contentLengthHeader.getValue());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user