|
@@ -1,24 +1,25 @@
|
|
|
package com.miaxis.test.feign;
|
|
|
|
|
|
import com.miaxis.HzgzptApplication;
|
|
|
+import com.miaxis.common.sign.*;
|
|
|
import com.miaxis.feign.IQgptService;
|
|
|
import com.miaxis.test.Impl.TestServiceImpl;
|
|
|
-import com.miaxis.common.sign.ISign;
|
|
|
-import com.miaxis.common.sign.PlatUtil;
|
|
|
-import com.miaxis.common.sign.Sign;
|
|
|
-import org.apache.commons.httpclient.methods.multipart.FilePart;
|
|
|
-import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
|
|
|
-import org.apache.commons.httpclient.methods.multipart.Part;
|
|
|
-import org.apache.commons.httpclient.params.HttpMethodParams;
|
|
|
+import lombok.SneakyThrows;
|
|
|
+import org.apache.commons.fileupload.FileItem;
|
|
|
+import org.apache.commons.fileupload.FileItemFactory;
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItem;
|
|
|
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
|
import org.junit.Test;
|
|
|
import org.junit.runner.RunWith;
|
|
|
import org.springframework.boot.test.context.SpringBootTest;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
-import java.io.File;
|
|
|
-import java.io.FileInputStream;
|
|
|
-import java.io.InputStream;
|
|
|
+import java.io.*;
|
|
|
import java.security.KeyStore;
|
|
|
import java.security.PrivateKey;
|
|
|
import java.security.cert.X509Certificate;
|
|
@@ -41,6 +42,24 @@ public class FeignTest {
|
|
|
|
|
|
@Resource
|
|
|
private IQgptService qgptService;
|
|
|
+ @Test
|
|
|
+ @SneakyThrows
|
|
|
+ public void testHandleFileUpload() {
|
|
|
+
|
|
|
+ File file = new File("upload.txt");
|
|
|
+ DiskFileItem fileItem = (DiskFileItem) new DiskFileItemFactory().createItem("file",
|
|
|
+ MediaType.TEXT_PLAIN_VALUE, true, file.getName());
|
|
|
+
|
|
|
+ try (InputStream input = new FileInputStream(file); OutputStream os = fileItem.getOutputStream()) {
|
|
|
+ IOUtils.copy(input, os);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new IllegalArgumentException("Invalid file: " + e, e);
|
|
|
+ }
|
|
|
+
|
|
|
+ MultipartFile multi = new CommonsMultipartFile(fileItem);
|
|
|
+
|
|
|
+// log.info(uploadClient.handleFileUpload(multi));
|
|
|
+ }
|
|
|
|
|
|
|
|
|
@Test
|
|
@@ -64,20 +83,43 @@ public class FeignTest {
|
|
|
ISign sign = new Sign();
|
|
|
byte[] data = PlatUtil.getBytesFromFile(file);
|
|
|
sign_str = sign.sign(data, timestamp, privateKey);
|
|
|
-
|
|
|
/* 验证签名 */
|
|
|
X509Certificate cert = (X509Certificate) keyStore.getCertificate(alias);
|
|
|
String serialNumber = cert.getSerialNumber().toString(); // 证书序列号
|
|
|
long snInteger = Long.parseLong(serialNumber);
|
|
|
serialNumber = Long.toHexString(snInteger).toUpperCase();
|
|
|
+ MultipartFile multipartFile = fileToMultipartFile(file);
|
|
|
+ Map resultMap = qgptService.imageup(PlatUtil.url_version, timestamp, serialNumber, sign_str,multipartFile);
|
|
|
+ System.out.println(resultMap);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ public static MultipartFile fileToMultipartFile(File file) {
|
|
|
+ String fieldName = "file"; //重点 这个名字需要和你对接的MultipartFil的名称一样
|
|
|
+ FileItemFactory factory = new DiskFileItemFactory(16, null);
|
|
|
+ FileItem item = factory.createItem(fieldName, "multipart/form-data", true, file.getName());
|
|
|
+ int bytesRead = 0;
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
+ try {
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
+ OutputStream os = item.getOutputStream();
|
|
|
+ while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
+ }
|
|
|
+ os.close();
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return new CommonsMultipartFile(item);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test3() throws Exception {
|
|
|
+
|
|
|
|
|
|
- FilePart fp = new FilePart("file", file);
|
|
|
- Part[] parts = { fp };
|
|
|
- MultipartRequestEntity entity = new MultipartRequestEntity(parts,new HttpMethodParams());
|
|
|
|
|
|
- Map resultMap = qgptService.imageup(PlatUtil.url_version, System.currentTimeMillis(), serialNumber, sign_str, entity);
|
|
|
- System.out.println(resultMap);
|
|
|
- //System.out.println(testService.imageUpForQG(file,this.httpServer_Imageup,this.file_stuimg,""));
|
|
|
}
|
|
|
|
|
|
|