Skip to content

Commit

Permalink
1、添加默认值功能
Browse files Browse the repository at this point in the history
  • Loading branch information
HHa1ey committed May 6, 2023
1 parent 4e6f019 commit 29f4539
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 63 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
## 运行环境

JDK 1.8
打包命令:mvn package assembly:single

打包命令:mvn package

## 工具界面

Expand Down
73 changes: 44 additions & 29 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,36 +34,51 @@
<version>8.9</version>
</dependency>
</dependencies>


<build>
<pluginManagement>
<plugins>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>


<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.ha1ey.CandleDragon.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<finalName>CandleDragon-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
<archive>
<manifest>
<mainClass>com.ha1ey.CandleDragon.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<outputDirectory>
${project.build.directory}
</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
56 changes: 51 additions & 5 deletions src/main/java/com/ha1ey/CandleDragon/plugin/http/HttpTool.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package com.ha1ey.CandleDragon.plugin.http;


import com.ha1ey.CandleDragon.tools.Tools;
import com.ha1ey.CandleDragon.ui.MainController;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.Proxy;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.*;
import java.nio.file.Files;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.HashMap;
import java.util.Map;

public class HttpTool {

Expand Down Expand Up @@ -45,7 +45,7 @@ public static Response get(String url, HashMap<String, String> headers, int conn
public static Response post(String url, HashMap<String, String> headers, String postStr, int connectTimeout) {
Response response = new Response(0, null, null, null);
try {
HttpURLConnection conn = getConn(url, connectTimeout);
HttpURLConnection conn = getConn(url,connectTimeout);
conn.setRequestMethod("POST");

for (String key : headers.keySet()) {
Expand All @@ -62,6 +62,52 @@ public static Response post(String url, HashMap<String, String> headers, String
return response;
}

public static Response sendFile(String url, HashMap<String,String> headers, Map<String,String> params, String fileKey, String filePath, String reFileName, int connectTimeout) {
Response response = new Response(0, null, null, null);
String boundary = "------WebKitFormBoundary" + Tools.randomStr(16);
try {
HttpURLConnection conn = getConn(url, connectTimeout);
File file = new File(filePath);
if (reFileName.equals("")) {
reFileName = file.getName();
}
conn.setRequestMethod("POST");
for (String key : headers.keySet()) {
conn.setRequestProperty(key, headers.get(key));
}
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
conn.setRequestProperty("Content-Length", String.valueOf(file.length()));
OutputStream outputStream = conn.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream, MainController.charset), true);
for (Map.Entry<String, String> entry : params.entrySet()) {
writer.append("--").append(boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append("\"").append("\r\n");
writer.append("\r\n");
writer.append(entry.getValue()).append("\r\n");
writer.flush();
}
writer.append("--").append(boundary).append("\r\n");
writer.append("Content-Disposition: form-data; name=\"").append(fileKey).append("\"; filename=\"").append(reFileName).append("\"").append("\r\n");
writer.append("Content-Type: ").append(Files.probeContentType(file.toPath())).append("\r\n");
writer.append("\r\n");
writer.flush();
FileInputStream fileInputStream = new FileInputStream(file);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = fileInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
fileInputStream.close();
writer.append("\r\n").flush();
writer.append("--").append(boundary).append("--").append("\r\n");
writer.close();
response = getResponse(conn);
}catch (Exception var9){
response.setError(var9.getMessage());
}
return response;
}

private static Response getResponse(HttpURLConnection conn) {

Response response = new Response(0, null, null, null);
Expand Down
Loading

0 comments on commit 29f4539

Please sign in to comment.