springmvc多附件分发多系统

Posted 六楼外的风景

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springmvc多附件分发多系统相关的知识,希望对你有一定的参考价值。

  1. 1.需求:

近来运营商要对新开户用户做拍照留存并在人脸和活体识别,这功能涉及到很多个系统之关的交互,流程也比较长.因为原来boss系统用的技术都比较老旧,决定对现有项目进行扩展,选用了比较常用的springmvc.本来一切比较顺利,但到了用httpclient实现多附件各大系统之间做传送这个地方出了点小问题,记录下来,希望能给大家一点帮助!

  1. 2.springmvc配置

下面是一spring引入文件上传配置,另外要多加两个jar包commons-fileupload-1.2.2.jar, commons-io-2.1.jar

<!-- 设置multipartResolver文件上传 -->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="5000000000"></property>
    </bean>

CommonsMultipartResolver其它参数设置自行google!

  1. 3 httpclient简单模拟多附件无序调用
public class HttpClientFiles 
    public static void main(String[] args) throws ClientProtocolException, IOException 
    String url = "http://localhost:8080/xxx/xxx";
        String filePath = new String("D:/log/images.jpg");
        String filePath2 = new String("D:/log/images2.jpg");
        File file = new File(filePath);
        File file2 = new File(filePath2);
        HttpClient httpclient = new DefaultHttpClient();
        int statusCode=0;
        HttpPost httppost = new HttpPost(url);
        FileBody fileBody = new FileBody(file);
        FileBody fileBody2 = new FileBody(file2);
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("attachs", fileBody);
        reqEntity.addPart("attachs", fileBody2);
        httppost.setEntity(reqEntity);
        HttpResponse responseBack = httpclient.execute(httppost);
        statusCode = responseBack.getStatusLine().getStatusCode();
        System.out.println("statusCode--------------------------------->"+statusCode);
    

服务器代码

 @RequestMapping(value = "/uploadFile",method=RequestMethod.POST)  
    public String upload(@RequestParam("attachs") MultipartFile[] attachs, HttpServletRequest request,HttpServletResponse resp) throws IOException 
    System.out.println(attachs.length);
        String url = GDIPUtil.paperLess;
        logger.info("转发app-->无纸化---活体验证图片:"+url);
        if (GDIPUtil.timeOut!=null) 
         tOut=Integer.parseInt(GDIPUtil.timeOut);
                       //http发送
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try 
            //String url = "http://127.0.0.1:8080/gdunicom/face/uploadPhoto.action";
            HttpPost httpPost = new HttpPost(url);
            ContentType ct = ContentType.create("text/plain","UTF-8");
            MultipartEntity entity = new MultipartEntity();  
            for (int i = 0; i < attachs.length; i++) 
                entity.addPart(attachs[0].getName(),new InputStreamBody(attachs[0].getInputStream(),
                        attachs[0].getContentType(),attachs[0].getOriginalFilename()));
                logger.info("img1:文件"+attachs[0].getOriginalFilename());
                logger.info("img1:文件"+attachs[0].getName());
            

问题来了,这个MultipartFile[] attachs 数组无论传多少个附件都 只有一个,查了很久,原来是原来项目中的spring版本是xxxx.3.0.0.RELEASE的一个bug,后面更换了版本到xxxx.-3.1.3.RELEASE版本就可以了,为了兼容原有的spring版本,这个版本还不能升级到3.2以上的,要不一启动就报各种方法找不到,从这也可以看出spring3.2以上的版本是不向下兼容的!

  1. 4 httpclient简单模拟多附件有序调用
    这个有序是相对于以上的无序附件而言,无序是指并没有指定文件名字段或者说是只指定了一个字段(如上attachs),简单的理解就是不知要传多少个附件,就用一个数组来装;但在分发系统中一般要指定哪几个字段来传附件,传多少个附件,这样他们才能按约定字段接收!

httpClient客户端代码(大部分同上,关键代码),指定img1,img2,verifyPackage字段!

File file = new File(filePath);
            File file2 = new File(filePath2);
            File file3 = new File(filePath3);
            FileBody bin = new FileBody(file);
            FileBody bin2 = new FileBody(file2);
            FileBody bin3 = new FileBody(file3);
            ContentType ct = ContentType.create("text/plain","UTF-8");
            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .addPart("img1", bin)
                    .addPart("img2", bin2)
                    .addPart("verifyPackage", bin3)

服务器端接收

@RequestMapping(value = "/upPic",method=RequestMethod.POST)  
        public String upPic(@RequestParam("img1") MultipartFile img1, 
                @RequestParam("img2") MultipartFile img2, 
                @RequestParam("verifyPackage") MultipartFile verifyPackage, 
                HttpServletRequest request,HttpServletResponse resp) throws IOException 
            String url = GDIPUtil.paperLess;
            CloseableHttpClient httpClient = HttpClients.createDefault();
            CloseableHttpResponse response = null;
            try 
                HttpPost httpPost = new HttpPost(url);
                ContentType ct = ContentType.create("text/plain","UTF-8");
                //设置超时
                RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout(tOut).setConnectTimeout(tOut).build();
                httpPost.setConfig(requestConfig);
                MultipartEntity entity = new MultipartEntity();

                if (img1!=null) 
                    entity.addPart("img1",new InputStreamBody(img1.getInputStream(),img1.getContentType(),img1.getOriginalFilename()));
                    logger.info("img1:文件"+img1.getOriginalFilename());
                    logger.info("img1:文件"+img1.getName());
                

                if (img2!=null) 
                    entity.addPart("img2",new InputStreamBody(img2.getInputStream(),img2.getContentType(),img2.getOriginalFilename()));
                    logger.info("img2:文件"+img2.getOriginalFilename());
                    logger.info("img2:文件"+img2.getName());
                
                if (verifyPackage!=null) 
                    entity.addPart("verifyPackage",new InputStreamBody(verifyPackage.getInputStream(),verifyPackage.getContentType(),verifyPackage.getOriginalFilename()));
                    logger.info("verifyPackage:文件"+verifyPackage.getOriginalFilename());
                    logger.info("verifyPackage:文件"+verifyPackage.getName());
                

这是按顺指定文件名接收,测试均可成功的!
另外补充一点关键点
客户端生成文件流是这个
File file = new File(filePath);
org.apache.http.entity.mime.content.StringBody.FileBody bin = new FileBody(file);
但在服务器端我们不应该存成图片,然后再按路径生成文件分法,找了FileBody父类实现还有个InputStreamBody,

 public InputStreamBody(InputStream in, ContentType contentType, String filename)
    
        super(contentType);
        Args.notNull(in, "Input stream");
        this.in = in;
        this.filename = filename;
    

这个就可以把上传文件输入流放到这个输出流当中,实现不存图片转发!
生产上httpclient实现还有很多参数要注意,这个看后继写个专门文章!

以上是关于springmvc多附件分发多系统的主要内容,如果未能解决你的问题,请参考以下文章

java如何实现批量发送邮件

spring schedule的线程与重入问题

Android 调用系统Email发送带多附件的邮件

多附件上传

我可以将 S/MIME 作为多部分/混合消息的一部分吗?

不惧恶意***,自带活体检测的人脸识别已上线!