zoukankan      html  css  js  c++  java
  • Java模拟表单POST上传文件

    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 = " "; 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"); }
  • 相关阅读:
    安装APK失败,错误代码:INSTALL_FAILED_INVALID_APK 解决方案
    android保持服务不休眠(持续运行)以及唤醒屏幕的方法
    判断Android 当前版本是否为debug版本
    Android 使用WebView加载含有Canvas的页面截屏处理
    喜大普奔,微软Microsoft JDBC Driver For SQL Server已发布到maven中央仓库
    系统架构设计理论与原则、负载均衡及高可用系统设计速记
    Sharing A Powerful Tool For Application Auto Monitor
    Sharing A Powerful Tool For Calculate Code Lines
    关于GC和析构函数的一个趣题
    垃圾回收机制GC知识再总结兼谈如何用好GC
  • 原文地址:https://www.cnblogs.com/TheoryDance/p/11903762.html
Copyright © 2011-2022 走看看