parent
f191991fdf
commit
6d42bb122b
@ -0,0 +1,77 @@ |
|||||||
|
package com.dxhy.order.consumer.utils; |
||||||
|
|
||||||
|
import org.apache.http.HttpEntity; |
||||||
|
import org.apache.http.client.methods.CloseableHttpResponse; |
||||||
|
import org.apache.http.client.methods.HttpPost; |
||||||
|
import org.apache.http.entity.ContentType; |
||||||
|
import org.apache.http.entity.mime.HttpMultipartMode; |
||||||
|
import org.apache.http.entity.mime.MultipartEntityBuilder; |
||||||
|
import org.apache.http.impl.client.CloseableHttpClient; |
||||||
|
import org.apache.http.impl.client.HttpClients; |
||||||
|
import org.apache.http.util.EntityUtils; |
||||||
|
import org.springframework.beans.factory.annotation.Value; |
||||||
|
import org.springframework.cloud.context.config.annotation.RefreshScope; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import java.io.File; |
||||||
|
import java.io.FileInputStream; |
||||||
|
|
||||||
|
/** |
||||||
|
* @Description: 文件存储工具类 |
||||||
|
* @Author liufeilong |
||||||
|
* @Date 2023-02-09 |
||||||
|
**/ |
||||||
|
@Component |
||||||
|
@RefreshScope |
||||||
|
public class OpentextUtils { |
||||||
|
|
||||||
|
public static String url; |
||||||
|
@Value("${order.opentext.url}") |
||||||
|
public void setUrl(String urlParam){ |
||||||
|
url=urlParam; |
||||||
|
} |
||||||
|
public static String userName; |
||||||
|
@Value("${order.opentext.userName}") |
||||||
|
public void setUserName(String userNameParam){ |
||||||
|
userName = userNameParam; |
||||||
|
} |
||||||
|
public static String password; |
||||||
|
@Value("${order.opentext.password}") |
||||||
|
public void setPassword(String passwordParam){ |
||||||
|
password = passwordParam; |
||||||
|
} |
||||||
|
|
||||||
|
public static String uploadFile(){ |
||||||
|
String sTestFile = "C:\\这是一个测试.txt"; |
||||||
|
CloseableHttpClient httpClient = HttpClients.createDefault(); |
||||||
|
|
||||||
|
HttpPost uploadFile = new HttpPost(url); |
||||||
|
|
||||||
|
// set header, USEROTAS and INIT1234 are fixed.
|
||||||
|
uploadFile.setHeader("USERNAME", userName); |
||||||
|
uploadFile.setHeader("PASSWORD", password); |
||||||
|
|
||||||
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); |
||||||
|
builder.setMode(HttpMultipartMode.RFC6532); |
||||||
|
|
||||||
|
// add file to request
|
||||||
|
File f = new File(sTestFile); |
||||||
|
try { |
||||||
|
builder.addBinaryBody("file", new FileInputStream(f), ContentType.DEFAULT_BINARY, f.getName()); |
||||||
|
|
||||||
|
HttpEntity multipart = builder.build(); |
||||||
|
uploadFile.setEntity(multipart); |
||||||
|
CloseableHttpResponse response; |
||||||
|
|
||||||
|
response = httpClient.execute(uploadFile); |
||||||
|
HttpEntity responseEntity = response.getEntity(); |
||||||
|
String sResponse = ""; |
||||||
|
sResponse = EntityUtils.toString(responseEntity, "UTF-8"); |
||||||
|
System.out.println(sResponse); |
||||||
|
} catch (Exception e) { |
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace(); |
||||||
|
} |
||||||
|
return ""; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue