菜得不要太明显 2021-07-27 11:08 采纳率: 0%
浏览 29

java 中的 MultipartEntity 类上传图片 在 php 中应该如何实现

java 中的MultipartEntity类上传图片 在php 中应该如何实现


    public static String sendHttpMessage(String url,Map<String, String> map, File file,String fileKey) throws Exception{
        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        HttpPost post = null;
        try {
            if(StringUtil.isEmpty(url) || null == map || map.isEmpty()){
                return null;
            }
            //创建POST请求
            post = new HttpPost(url);
            post.setHeader("Server","nginx/1.14.2");
            MultipartEntity entity = new MultipartEntity();  
            //请求参数
            for(Entry<String,String> entry : map.entrySet()){
//                params.add(new BasicNameValuePair(key,map.get(key))); 
                entity.addPart(entry.getKey(), new StringBody(entry.getValue(), Charset.forName("UTF-8")));  
            }
            entity.addPart(fileKey,new FileBody(file));  
            post.setEntity(entity);  
            //发�?�请�?
             client = HttpClients.createDefault();
             response = client.execute(post);
            if(response.getStatusLine().getStatusCode() != HttpStatus.SC_OK){
                throw new Exception("请求失败�?");
            }
            
            HttpEntity resEntity = response.getEntity();
            return null == resEntity ? "" : EntityUtils.toString(resEntity,CHARSET);
        }  catch (Exception e) {
            log.info("异常�?"+e);
            throw e;
        }
    finally {
         try{
             if(response != null){
                 response.close();
            }
             if(client != null){
                 client.close();
            }
             if (post != null) {
                post.releaseConnection();
            }
         } catch (IOException e) {
             log.error(e.getMessage(), e);
         }
        
    }
    }
  • 写回答

2条回答 默认 最新

  • 关注

    MultipartEntity对象是servlet API3.0开始支持的,php有对应的文件上传技术。

    评论

报告相同问题?

问题事件

  • 创建了问题 7月27日

悬赏问题

  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?