java 接口 文件传输

Posted xiufengd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 接口 文件传输相关的知识,希望对你有一定的参考价值。

调用接收端

    @ApiOperation(value = "文件请求展示方法")
    @RequestMapping(value = "/showFile", method = RequestMethod.GET)
    public void showFile(HttpServletRequest request, HttpServletResponse response) throws Exception 
        //获取接口url
        String url = "http://127.0.0.1/a/b";
        //然后根据表名获取公司信息
        HttpPost httppost = new HttpPost(url);
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        params.add(new BasicNameValuePair("comName", "111111"));
        HttpResponse httpResponse = null;
        HttpEntity httpEntity = null;
        try 
            httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            RequestConfig requestConfig = RequestConfig.custom()  
                    .setConnectTimeout(30000).setConnectionRequestTimeout(30000)  
                    .setSocketTimeout(30000).build(); 
            httppost.setConfig(requestConfig);
            HttpClient httpclient = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler()).build(); 
            httpResponse = httpclient.execute(httppost);
        catch (Exception e1) 
            logger.error("----------------失败");
        
        if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) // 请求正常
            try 
                httpEntity = httpResponse.getEntity();
                BufferedInputStream br = new BufferedInputStream(httpEntity.getContent());
                byte[] buf = new byte[1024];
                int len = 0;
                response.reset(); // 非常重要
                response.setContentType("application/pdf");
                String fileName ="report.pdf";
                try 
                    fileName = httpResponse.getAllHeaders()[5].getValue().split(";")[1].split("=")[1];
                 catch (Exception e) 
                
                        
                response.setHeader("Content-Disposition",
                        "inline; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));

                OutputStream out = response.getOutputStream();
                while ((len = br.read(buf)) != -1)
                    out.write(buf, 0, len);
                br.close();
                out.flush();

             catch (Exception e) 
                logger.error("解析失败");
            
        else 
            logger.error("调用失败");
        
        
    

文件存储方

   @ApiOperation(value = "文件支持接口")
    @RequestMapping(value = "/getReport", method = RequestMethod.GET)
    public Object getReport(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) 
        Map<String, Object> params = WebUtil.getParameterMap(request);
        String comName = (String) params.get("comName");
        File file = new File("C:\\Users\\Administrator\\Desktop\\滴滴电子发票.pdf");
        
        try 
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition",
                    "inline; filename=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
         catch (UnsupportedEncodingException e) 
            e.printStackTrace();
        
        
        try 
            ServletOutputStream pw = response.getOutputStream();
            BufferedInputStream br = new BufferedInputStream(new FileInputStream(file));
            byte[] by = new byte[(int) file.length()];
            while(br.read(by)!=-1)
                pw.write(by);
            
            pw.flush();
         catch (IOException e1) 
            e1.printStackTrace();
        
        
        return null;
    

 

以上是关于java 接口 文件传输的主要内容,如果未能解决你的问题,请参考以下文章

ssm调用webservice接口并进行文件传输

java的socket传输文件,发送与接收文件的内容不一致

java如何在网络流中传输对象

java传输大文件?

java 传输 获取文件类型

java中UDP文件传输怎么实现?