文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

在Java项目中如果发送http请求实现文件上传功能

2023-05-31 13:26

关注

在Java项目中如果发送http请求实现文件上传功能?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

具体代码如下所示:

package wxapi.WxHelper; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.Map; import java.util.Map.Entry; public class HttpRequestUtil {      public static String sendGet(String requestUrl, Map<String, String> requestHeader, String responseEncoding) {     String result = "";     BufferedReader reader = null;     try {       if (requestUrl == null || requestUrl.isEmpty()) {         return result;       }       URL realUrl = new URL(requestUrl);       URLConnection connection = realUrl.openConnection();       connection.setRequestProperty("accept", "text/html, application/xhtml+xml, image/jxr, *   public static String sendPost(String requestUrl, Map<String, String> requestHeader, Map<String, String> formTexts, Map<String, String> files, String requestEncoding, String responseEncoding) {     OutputStream out = null;     BufferedReader reader = null;     String result = "";     try {       if (requestUrl == null || requestUrl.isEmpty()) {         return result;       }       URL realUrl = new URL(requestUrl);       HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();       connection.setRequestProperty("accept", "text/html, application/xhtml+xml, image/jxr, */*");       connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");       if (requestHeader != null && requestHeader.size() > 0) {         for (Entry<String, String> entry : requestHeader.entrySet()) {           connection.setRequestProperty(entry.getKey(), entry.getValue());         }       }       connection.setDoOutput(true);       connection.setDoInput(true);       connection.setRequestMethod("POST");       if (requestEncoding == null || requestEncoding.isEmpty()) {         requestEncoding = "UTF-8";       }       if (responseEncoding == null || responseEncoding.isEmpty()) {         responseEncoding = "UTF-8";       }       if (requestHeader != null && requestHeader.size() > 0) {         for (Entry<String, String> entry : requestHeader.entrySet()) {           connection.setRequestProperty(entry.getKey(), entry.getValue());         }       }       if (files == null || files.size() == 0) {         connection.setRequestProperty("content-type", "application/x-www-form-urlencoded");         out = new DataOutputStream(connection.getOutputStream());         if (formTexts != null && formTexts.size() > 0) {           String formData = "";           for (Entry<String, String> entry : formTexts.entrySet()) {             formData += entry.getKey() + "=" + entry.getValue() + "&";           }           formData = formData.substring(0, formData.length() - 1);           out.write(formData.toString().getBytes(requestEncoding));         }       } else {         String boundary = "-----------------------------" + String.valueOf(new Date().getTime());         connection.setRequestProperty("content-type", "multipart/form-data; boundary=" + boundary);         out = new DataOutputStream(connection.getOutputStream());         if (formTexts != null && formTexts.size() > 0) {           StringBuilder sbFormData = new StringBuilder();           for (Entry<String, String> entry : formTexts.entrySet()) {             sbFormData.append("--" + boundary + "\r\n");             sbFormData.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r\n\r\n");             sbFormData.append(entry.getValue() + "\r\n");           }           out.write(sbFormData.toString().getBytes(requestEncoding));         }         for (Entry<String, String> entry : files.entrySet()) {           String fileName = entry.getKey();           String filePath = entry.getValue();           if (fileName == null || fileName.isEmpty() || filePath == null || filePath.isEmpty()) {             continue;           }           File file = new File(filePath);           if (!file.exists()) {             continue;           }           out.write(("--" + boundary + "\r\n").getBytes(requestEncoding));           out.write(("Content-Disposition: form-data; name=\"" + fileName + "\"; filename=\"" + file.getName() + "\"\r\n").getBytes(requestEncoding));           out.write(("Content-Type: application/x-msdownload\r\n\r\n").getBytes(requestEncoding));           DataInputStream in = new DataInputStream(new FileInputStream(file));           int bytes = 0;           byte[] bufferOut = new byte[1024];           while ((bytes = in.read(bufferOut)) != -1) {             out.write(bufferOut, 0, bytes);           }           in.close();           out.write(("\r\n").getBytes(requestEncoding));         }         out.write(("--" + boundary + "--").getBytes(requestEncoding));       }       out.flush();       out.close();       out = null;       reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), responseEncoding));       String line;       while ((line = reader.readLine()) != null) {         result += line;       }     } catch (Exception e) {       System.out.println("发送POST请求出现异常!");       e.printStackTrace();     } finally {       try {         if (out != null) {           out.close();         }         if (reader != null) {           reader.close();         }       } catch (IOException ex) {         ex.printStackTrace();       }     }     return result;   } } 

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 资料下载
  • 历年真题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     807人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     351人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     314人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     433人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     221人已做
    查看

相关文章

发现更多好内容
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯