commons-httpclient commons-httpclient 3.1
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-parent</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.2.RELEASE</version>
</dependency>
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.httpclient.methods.multipart.Part;
public class FileLoadClient {
private static final int TIMEOUT = 5000;
public static void main(String[] args) {
fileload("http://localhost:8080/uploadFile", new File("D:\\google-collections\\1.0\\google-collections-1.0.jar"));
}
public static String fileload(String url, File file) {
String body = "{}";
if (url == null || url.equals("")) {
return "illegal";
}
if (!file.exists()) {
return "file not exist";
}
PostMethod postMethod = new PostMethod(url);
BufferedReader br = null;
try {
FilePart fp = new FilePart("file", file);
Part[] parts = {fp};
MultipartRequestEntity mre = new MultipartRequestEntity(parts, postMethod.getParams());
postMethod.setRequestEntity(mre);
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(TIMEOUT);
int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
InputStream inputStream = postMethod.getResponseBodyAsStream();
br = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
String str = "";
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
body = stringBuffer.toString();
} else {
body = "fail";
}
} catch (Exception e) {
} finally {
if (br != null)
try {
br.close();
} catch (IOException e) {
}
postMethod.releaseConnection();
}
return body;
}
}
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class UploadFile {
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public String uploadFile(HttpServletResponse response, @RequestParam("file") MultipartFile file) {
if (file != null && !file.isEmpty()) {
try {
String filePath = file.getOriginalFilename();
InputStream inputStream = null;
FileOutputStream outputStream = null;
try {
inputStream = file.getInputStream();
File f = new File(filePath);
f.createNewFile();
byte[] bom = new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF};
outputStream = new FileOutputStream(f);
if (filePath.endsWith(".txt")) {
outputStream.write(bom);
}
byte temp[] = new byte[1024];
int size = -1;
while ((size = inputStream.read(temp)) != -1) {
outputStream.write(temp, 0, size);
}
response.getWriter().write("upload success");
} catch (IOException e) {
return e.getMessage();
} finally {
outputStream.close();
inputStream.close();
}
} catch (Exception e) {
return e.getMessage();
}
}
return "File is Empty";
}
}
|