JAVA模拟表单POST上传文件

import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.concurrent.Callable;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageTypeSpecifier;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;

import org.apache.commons.lang.StringUtils;

import com.grand.mysql_handler.mapper.SystemMapper;

import net.sf.json.JSONObject;


private String uploadImage(String name,byte[] buf) throws Exception { String filename = name.substring(name.lastIndexOf("/") + 1); final String newLine = "\r\n";
final String boundaryPrefix = "--";
final String boundary = "----theorydance"; String api_url = "http://localhost:8080/filestorage/app/api/fileUpload";
HttpURLConnection conn = (HttpURLConnection) new URL(api_url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setReadTimeout(20000);
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); StringBuilder sb = new StringBuilder(); // key参数
sb.append(boundaryPrefix + boundary + newLine);
sb.append("Content-Disposition: form-data; name=\"name\"" + newLine);
sb.append(newLine);
sb.append((name==null?"default":name) + newLine); // 图片数据
sb.append(boundaryPrefix + boundary + newLine);
sb.append("Content-Disposition: form-data; name=\"myfile\"; filename=\""+filename+"\"" + newLine);
sb.append("Content-Type: application/octet-stream" + newLine);
sb.append(newLine); OutputStream out = new DataOutputStream(conn.getOutputStream());
out.write(sb.toString().getBytes());
out.write(buf);
out.write(newLine.getBytes()); String endFlag = boundaryPrefix + boundary + boundaryPrefix + newLine;
out.write(endFlag.getBytes());
out.flush();
out.close(); System.out.println("响应状态码:"+conn.getResponseCode());
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line = null;
String content = "";
while((line=br.readLine())!=null) {
content += line;
}
JSONObject json = JSONObject.fromObject(content);
System.out.println(json.toString());
return json.getJSONObject("data").getString("url");
}

Java模拟表单POST上传文件的更多相关文章

  1. CURL模拟表单post提交及相关常用参数的使用(包括提交表单同时上传文件)

    转载自:https://blog.csdn.net/freedomwjx/article/details/43278157 (注:在curl前面加上time如time curl xxx,可以在最后显示 ...

  2. JAVA模拟HTTP post请求上传文件

    在开发中,我们使用的比较多的HTTP请求方式基本上就是GET.POST.其中GET用于从服务器获取数据,POST主要用于向服务器提交一些表单数据,例如文件上传等.而我们在使用HTTP请求时中遇到的比较 ...

  3. Servlet 表单及上传文件

    // 文件路径 D:\ApacheServer\web_java\HelloWorld\src\com\test\TestServletForm.java package com.test; impo ...

  4. web 表单方式上传文件方法(不用flash插件)

    原理:使用表单的input type="file"标签,通过ajax提交表单请求,后台获取请求中的文件信息,进行文件保存操作 由于我测试用的做了一个上传文件和上传图片方法,所以我有 ...

  5. ajax异步上传文件和表单同步上传文件 的区别

    1. 用表单上传文件(以照片为例)-同步上传 html部分代码:这里请求地址index.php <!DOCTYPE html> <html lang="en"&g ...

  6. php 利用fsockopen GET/POST 提交表单及上传文件

    1.GET get.php <?php $host = 'demo.fdipzone.com'; $port = 80; $errno = ''; $errstr = ''; $timeout  ...

  7. Java模拟客户端向服务器上传文件

    先来了解一下客户端与服务器Tcp通信的基本步骤: 服务器端先启动,然后启动客户端向服务器端发送数据. 服务器端收到客户端发送的数据,服务器端会响应应客户端,向客户端发送响应结果. 客户端读取服务器发送 ...

  8. php 利用 fsockopen GET/POST 提交表单及上传文件

    1.GET get.php <?php$host = 'demo.fdipzone.com';$port = 80;$errno = '';$errstr = '';$timeout = 30; ...

  9. JAVA入门[16]-form表单,上传文件

    一.如何传递参数 使用 @RequestParam 可以传递查询参数.例如:http://localhost:8092/category/detail?id=1 @RequestMapping(&qu ...

随机推荐

  1. mysql8.0参考手册学习

    mysql8.0参考手册链接:https://dev.mysql.com/doc/refman/8.0/en/optimizer-hints.html#optimizer-hints-join-ord ...

  2. rgw前端替换civetweb为beast

    前言 ceph的rgw现在提供了两种前端, civetweb和beast 配置 修改配置文件 rgw_frontends = civetweb port=7481 为 rgw frontends = ...

  3. ceph查询rbd的使用容量(快速)

    ceph在Infernalis加入了一个功能是查询rbd的块设备的使用的大小,默认是可以查询的,但是无法快速查询,那么我们来看看这个功能是怎么开启的 ceph版本 root@lab8107:~/cep ...

  4. API的使用(3)Arrays 类,Math类,三大特性--继承

    Arrays类 概述   java.util.Arrays此时主要是用来操作数组,里面提供了很多的操作API的方法.如[排序]和[搜索]功能.其所有的方法均为静态方法,调用起来非常简单. 操作数组的方 ...

  5. Guitar Pro吉他指弹入门——美式指弹

    说起指弹吉他,很多身边的琴友首先反应到的是押尾桑,岸部真明,伍伍慧等等指弹艺术家的日式指弹.笔者在初涉指弹的时候,也是如此,但是随着学习的加深,首先认识到了汤米大神(Tommy Emmanuel),然 ...

  6. Guitar Pro指弹入门——特殊拍号

    在吉他演奏技术不断提高的同时,我们经常会遇到一些奇怪的曲谱.他们的拍号不是正常的4/4拍或者3/4拍,而是5/4或者5/8等等我们不太了解的拍号,致使我们在演奏和练习之中陷入纷乱的节奏. 那么本期文章 ...

  7. Java8常用的内置函数式接口(一)Predicate、Consumer、Supplier、Function

    Java8常用的内置函数式接口(一) 简介 JDK 1.8 API中包含了很多内置的函数式接口.有些是在以前版本的Java中大家耳熟能详的,例如Comparator接口,或者Runnable接口.对这 ...

  8. yii2.0 关于 ActiveForm 中 checkboxList 的使用

    在视图中创建复选框,列出复选框内的内容其中$id 为 列出在复选框中的数组 //$ids:所有要显示的checkbox(Array)<?=$form->field($model, 'id' ...

  9. appium快速入门

    appium快速入门 演示官方demo 第一步:启动安卓模拟器 步骤2:启动Appium桌面 step3:准备自动化脚本与待测APK step4:运行测试代码 分析演示 分析Appium的加载流程 使 ...

  10. Go 大数据生态迎来重要产品 CDS

    项目地址:https://github.com/tal-tech/cds ClickHouse是一个用于联机分析(OLAP)的列式数据库管理系统(DBMS).它有着优异的性能,可以快速部署和运行. 不 ...