整合了一个自己写的采用Http做附件上传的工具,分享一下!
/** * 以Http协议传输文件 * * @author mingxue.zhang@163.com * */ public class HttpPostUtil { private final static char[] MULTIPART_CHARS = "-_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" .toCharArray(); private URL url; private HttpURLConnection conn; private String boundary = null; private Map<String, String> textParams = new HashMap<String, String>(); private Map<String, File> fileparams = new HashMap<String, File>(); public HttpPostUtil(String url) throws Exception { this.url = new URL(url); } // 重新设置要请求的服务器地址,即上传文件的地址。 public void setUrl(String url) throws Exception { this.url = new URL(url); } // 增加一个普通字符串数据到form表单数据中 public void addTextParameter(String name, String value) { textParams.put(name, value); } // 增加一个文件到form表单数据中 public void addFileParameter(String name, File value) { fileparams.put(name, value); } // 清空所有已添加的form表单数据 public void clearAllParameters() { textParams.clear(); fileparams.clear(); } /** * 发送数据到服务器 * * @return 一个字节包含服务器的返回结果的数组 * @throws Exception */ public byte[] send() throws Exception { initConnection(); try { conn.connect(); } catch (SocketTimeoutException e) { throw new Exception(e); } OutputStream connOutStream = new DataOutputStream( conn.getOutputStream()); writeFileParams(connOutStream); writeStringParams(connOutStream); writesEnd(connOutStream); InputStream responseInStream = conn.getInputStream(); ByteArrayOutputStream responseOutStream = new ByteArrayOutputStream(); int len; byte[] bufferByte = new byte[1024]; while ((len = responseInStream.read(bufferByte)) != -1) { responseOutStream.write(bufferByte, 0, len); } responseInStream.close(); connOutStream.close(); conn.disconnect(); byte[] resultByte = responseOutStream.toByteArray(); responseOutStream.close(); return resultByte; } // 文件上传的connection的一些必须设置 private void initConnection() throws Exception { StringBuffer buf = new StringBuffer("----"); Random rand = new Random(); for (int i = 0; i < 15; i++) { buf.append(MULTIPART_CHARS[rand.nextInt(MULTIPART_CHARS.length)]); } this.boundary = buf.toString(); conn = (HttpURLConnection) this.url.openConnection(); conn.setDoOutput(true); conn.setUseCaches(false); conn.setConnectTimeout(3 * 60 * 1000); // 连接超时为10秒 conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); } // 普通字符串数据 private void writeStringParams(OutputStream out) throws Exception { Set<String> keySet = textParams.keySet(); for (Iterator<String> it = keySet.iterator(); it.hasNext();) { String name = it.next(); String value = textParams.get(name); out.write(("--" + boundary + "\r\n").getBytes()); out.write(("Content-Disposition: form-data; name=\"" + name + "\"\r\n") .getBytes()); out.write(("\r\n").getBytes()); out.write((encode(value) + "\r\n").getBytes()); } } // 文件数据 private void writeFileParams(OutputStream out) throws Exception { Set<String> keySet = fileparams.keySet(); for (Iterator<String> it = keySet.iterator(); it.hasNext();) { String name = it.next(); File value = fileparams.get(name); out.write(("--" + boundary + "\r\n").getBytes()); out.write(("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + encode(value.getName()) + "\"\r\n") .getBytes()); out.write(("Content-Type: " + getContentType(value) + "\r\n") .getBytes()); out.write(("Content-Transfer-Encoding: " + "binary" + "\r\n") .getBytes()); out.write(("\r\n").getBytes()); FileInputStream inStream = new FileInputStream(value); int bytes = 0; byte[] bufferByte = new byte[1024]; while ((bytes = inStream.read(bufferByte)) != -1) { out.write(bufferByte, 0, bytes); } inStream.close(); out.write(("\r\n").getBytes()); } } // 添加结尾数据 private void writesEnd(OutputStream out) throws Exception { out.write(("--" + boundary + "--" + "\r\n").getBytes()); out.write(("\r\n").getBytes()); } // 获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream private String getContentType(File f) throws Exception { String fileName = f.getName(); if (fileName.endsWith(".jpg")) { return "image/jpeg"; } else if (fileName.endsWith(".png")) { return "image/png"; } return "application/octet-stream"; } // 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码 private String encode(String value) throws Exception { return URLEncoder.encode(value, "UTF-8"); } }
相关推荐
apache-jmeter-5.6.3 带插件
内置源程序供读者参考学习
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
MATLAB设计的图像去雾设计(GUI界面设计)
2023年江苏省计算机二级考试VB试题分类汇总及答案.pdf
Java 项目,仅供学习参考。 Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:jsp 后台框架:SSM 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4
2023年微型计算机原理试题库及答案.pdf
美联储在2024年9月18日宣布将其调50个基点,降至4.75%至5.00%之间的水平。这是美联储自2020年3月以来首次降息,也是自2023年7月将利率水平调升至历史高位后的首次下调,标志着货币政策由紧缩周期向宽松周期的转向 数据名称:美国联邦基金有效利率、目标利率历史数据 样本数量:12667条 数据年份:1990.1-2024.9 数据说明:包括有效利率、目标利率 更新日期:2024年9月
2023年专升本计算机基础习题答案.pdf
ava项目springboot基于springboot的课程设计,包含源码+数据库+毕业论文
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
Java 项目,仅供学习参考。 Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
Java 项目,仅供学习参考。 Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
【工程项目】MATLAB教室人数统计(GUI框架,定位和分割人脸,计数)
2023年山东专升本计算机真题及答案.pdf
2023年计算机与通信网络实验报告.pdf
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:jsp 后台框架:SSM 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4
Java 项目, Java 毕业设计,Java 课程设计,基于 SpringBoot 开发的,含有代码注释,新手也可看懂。毕业设计、期末大作业、课程设计、高分必看,下载下来,简单部署,就可以使用。 包含:项目源码、数据库脚本、软件工具等,前后端代码都在里面。 该系统功能完善、界面美观、操作简单、功能齐全、管理便捷,具有很高的实际应用价值。 项目都经过严格调试,确保可以运行! 1. 技术组成 前端:html、javascript、Vue 后台框架:SpringBoot 开发环境:idea 数据库:MySql(建议用 5.7 版本,8.0 有时候会有坑) 数据库工具:navicat 部署环境:Tomcat(建议用 7.x 或者 8.x 版本), maven 2. 部署 如果部署有疑问的话,可以找我咨询 Java工具包下载地址: https://pan.quark.cn/s/eb24351ebac4 后台路径地址:localhost:8080/项目名称/admin/dist/index.html 前台路径地址:localhost:8080/项目名称/front/index.html (无前台不需要输入)
英飞凌TC264代码-uart