Althars123 hace 4 años
padre
commit
82a54aeaa5

+ 2 - 1
hzgzpt-admin/src/main/java/com/miaxis/app/controller/report/ReportInfoController.java

@@ -1,5 +1,6 @@
 package com.miaxis.app.controller.report;
 
+import com.miaxis.feign.UploadDto;
 import com.miaxis.file.vo.FileVo;
 import com.miaxis.report.domain.ReportInfo;
 import com.miaxis.report.dto.ReportInfoDTO;
@@ -115,7 +116,7 @@ public class ReportInfoController extends BaseController{
     @Log(title = "上传举报照片", businessType = BusinessTypeEnum.INSERT)
     @PostMapping("/image")
     @ApiOperation("上传举报信息")
-    public Response<FileVo> uploadImage(MultipartFile multFile) throws IOException {
+    public Response<FileVo> uploadImage(MultipartFile multFile, @RequestBody UploadDto dto) throws IOException {
         String savePath = AliyunConfig.GZPT_REPORT
                 + new SimpleDateFormat("yyyyMMdd").format(new Date()) + "/report/";
         String fileName = System.currentTimeMillis() + ".jpg";//文件名

+ 106 - 0
hzgzpt-admin/src/main/java/com/miaxis/app/controller/test/TestController.java

@@ -0,0 +1,106 @@
+package com.miaxis.app.controller.test;
+
+import com.miaxis.common.constant.Constants;
+import com.miaxis.common.core.controller.BaseController;
+import com.miaxis.common.core.domain.Response;
+import com.miaxis.common.sign.*;
+import com.miaxis.news.domain.NewsInfo;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.apache.catalina.util.RequestUtil;
+import org.apache.commons.fileupload.FileItem;
+import org.apache.commons.fileupload.FileItemFactory;
+import org.apache.commons.fileupload.disk.DiskFileItemFactory;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.commons.CommonsMultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.*;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.security.cert.X509Certificate;
+import java.util.Enumeration;
+
+@RestController
+@RequestMapping(Constants.OPEN_PREFIX+"/test")
+@Api(tags={"【test-文件上传】"})
+    public class TestController extends BaseController {
+//        @Resource
+//        private IQgptService qgptService;
+
+    /**
+     * 查询新闻(首页资讯、通知、公告相关)列表
+     */
+    @PostMapping("/testMethod" )
+    @ApiOperation("文件上传" )
+    public Response list(HttpServletRequest request,@RequestParam("s") String s, @RequestBody NewsInfo newsInfo) throws Exception {
+       System.out.println( RequestUtil.getRequestURL(request));;
+        System.out.println("d");
+        return  null ;
+    }
+
+        /**
+         * 查询新闻(首页资讯、通知、公告相关)列表
+         */
+        @PostMapping("/fileupload" )
+        @ApiOperation("文件上传" )
+        public Response list(@RequestPart("file") MultipartFile file) throws Exception {
+//            File file =new File("F:/doc/img/gen-view.jpg");
+            long timestamp = System.currentTimeMillis(); // 时间戳
+            String sign_str = ""; // 签名
+            /* 获取签名 */
+            String path = getClass().getResource("/").getPath();
+            java.io.File jarFile = new java.io.File(path);
+            String propertiesfile = jarFile.getParentFile().getAbsolutePath().replace("%20", " ");
+            KeyStore keyStore = KeyStore.getInstance("PKCS12");
+            InputStream input = new FileInputStream(propertiesfile + PlatUtil.privateKeyUrl);
+            keyStore.load(input, PlatUtil.keyPassword.toCharArray());
+            Enumeration<String> aliases = keyStore.aliases();
+            if (!aliases.hasMoreElements()) {
+                throw new RuntimeException("HTTP接口协议签名获取失败: no alias found");
+            }
+            String alias = aliases.nextElement();
+            PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, PlatUtil.keyPassword.toCharArray()); // 私钥
+            ISign sign = new Sign();
+            byte[] data = file.getBytes();
+            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();
+            IVerify verify = new Verify();
+            boolean ok = verify.verify(data, timestamp, sign_str, cert);
+            System.out.println(ok);
+           // MultipartFile multipartFile = fileToMultipartFile(file);
+//        Map resultMap = qgptService.imageup(multipartFile);
+
+//            Map resultMap = qgptService.imageup(PlatUtil.url_version, String.valueOf(System.currentTimeMillis()), serialNumber, sign_str,file);
+//            System.out.println(resultMap);
+            return Response.success();
+        }
+
+    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);
+    }
+
+
+}

+ 1 - 0
hzgzpt-admin/src/main/resources/application.yml

@@ -199,5 +199,6 @@ sms_password: "t7nwh7"
 # 调用外部接口
 feign:
   jgpt_ipport: http://localhost:8080/
+#  qgpt_ipport: http://localhost:8080/
   qgpt_ipport: http://114.55.58.112:8085/
 

+ 59 - 17
hzgzpt-admin/src/test/java/com/miaxis/test/feign/FeignTest.java

@@ -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,""));
     }
 
 

+ 11 - 0
hzgzpt-common/pom.xml

@@ -187,6 +187,17 @@
             <artifactId>spring-cloud-starter-openfeign</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>io.github.openfeign.form</groupId>
+            <artifactId>feign-form</artifactId>
+            <version>3.8.0</version>
+        </dependency>
+        <dependency>
+            <groupId>io.github.openfeign.form</groupId>
+            <artifactId>feign-form-spring</artifactId>
+            <version>3.8.0</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 22 - 0
hzgzpt-common/src/main/java/com/miaxis/common/config/FeignConfig.java

@@ -0,0 +1,22 @@
+package com.miaxis.common.config;
+
+import feign.codec.Encoder;
+import feign.form.spring.SpringFormEncoder;
+import org.springframework.beans.factory.ObjectFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
+import org.springframework.cloud.openfeign.support.SpringEncoder;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class FeignConfig {
+
+    @Autowired
+    private ObjectFactory<HttpMessageConverters> messageConverters;
+
+    @Bean
+    public Encoder feignFormEncoder() {
+        return new SpringFormEncoder(new SpringEncoder(messageConverters));
+    }
+}

+ 1 - 1
hzgzpt-common/src/main/java/com/miaxis/common/sign/ISign.java

@@ -4,7 +4,7 @@ import java.security.PrivateKey;
 
 public interface ISign {
 	public String sign(String data, long timestamp, PrivateKey key) throws Exception;
-	public String sign(byte[] data, long timestamp, PrivateKey key) throws Exception;
 	public String sign(String data, PrivateKey key) throws Exception;
 	public String sign(byte[] data, PrivateKey key) throws Exception;
+	public String sign(byte[] data, long timestamp, PrivateKey key) throws Exception;
 }

+ 11 - 10
hzgzpt-service/src/main/java/com/miaxis/feign/IQgptService.java

@@ -1,24 +1,25 @@
 package com.miaxis.feign;
 
 
-import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
+import com.miaxis.common.config.FeignConfig;
 import org.springframework.cloud.openfeign.FeignClient;
-import org.springframework.web.bind.annotation.RequestBody;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.http.MediaType;
+import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
 import java.util.Map;
 
 @FeignClient(name="qgptService",
-        url = "${feign.qgpt_ipport}")
-
+        url = "${feign.qgpt_ipport}",configuration = FeignConfig.class)
+@Component
 public interface IQgptService {
-    @RequestMapping(value = "/top/imageup/stuimg", method = RequestMethod.POST)
-    Map imageup(@RequestParam("v" ) String version,
+    @PostMapping(value = "/top/imageup/stuimg", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
+    Map imageup(
+            @RequestParam("v" ) String version,
                 @RequestParam("ts" ) Long timestamp,
                 @RequestParam("user" ) String serialNumber,
                 @RequestParam("sign" ) String sign,
-                @RequestBody MultipartRequestEntity entity);
+                @RequestPart("file") MultipartFile file);
 
 }

+ 27 - 0
hzgzpt-service/src/main/java/com/miaxis/feign/UploadDto.java

@@ -0,0 +1,27 @@
+package com.miaxis.feign;
+
+import com.miaxis.common.core.domain.BaseBusinessEntity;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * @Author wwl
+ * @Date 2020/12/24
+ * @Version 1.0
+ */
+@Data
+public class UploadDto extends BaseBusinessEntity {
+
+    @ApiModelProperty(value = "姓名")
+    private String name;
+
+    @ApiModelProperty(value = "性别")
+    private Long sex;
+
+    @ApiModelProperty(value = "身份证明号码 1:身份证号,2:护照,3:军官证,4:其他")
+    private Long cardtype;
+
+    @ApiModelProperty(value = "身份证明号码")
+    private String idcard;
+
+}

+ 1 - 0
hzgzpt-service/src/main/java/com/miaxis/test/Impl/TestServiceImpl.java

@@ -35,6 +35,7 @@ public class TestServiceImpl implements ITestService {
         result = this.getHttpServerUrlForFileUpload(this.qgptHttpTopServerPath, serviceName + "/" + type, PlatUtil.getBytesFromFile(file), filePath);
         if (result != null && result.getResult().equals("0")) {
             String sendUrl = result.getObject().toString();
+            System.out.println(sendUrl);
             Part[] parts = { fp };
             PostMethod pomethod = new PostMethod(sendUrl);
             MultipartRequestEntity mre = new MultipartRequestEntity(parts, pomethod.getParams());