java模拟表单上传图片简单明了版本个人独创


package duo.xian.cheng.mo.ni;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import org.json.JSONObject;

import com.company.moniApiLogin.downimage;

public class MyTest {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
    	
        final String boundaryString = "--------------------------" + java.util.UUID.randomUUID().toString();
        final String end = "\r\n";
        final String twoHyphens = "--";
       // URL url = new URL("http://localhost:8080/SoyaApi_New/Api/Shop_BaseProductController/uploadBaseProductPhoto?baseProductId=009d65b6827bf06fbd64b617052d1d7d" +
      //  		"&type=1");
        URL url = new URL("http://localhost:8080/GeographyApi/Api/Tbl_PhotoController/addPhoto?type=1");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type",
                "application/x-www-form-urlencoded;charset=UTF-8");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundaryString);       
        connection.setRequestProperty("linkmanId", "7ba7902da69274f07e3847937a99ae5c");
        connection.setRequestProperty("company", "2d4ee76b4e1e4cd8a1e6a34fa79cd9fb");
        connection.setRequestProperty("token", "27a41134b22607e1ba5e7570b12ecccf");
        connection.setRequestProperty("baseProductId", "009d65b6827bf06fbd64b617052d1d7d");
        connection.setRequestProperty("type", "1");
       
        connection.connect();
        StringBuilder sbHeader = new StringBuilder();
        sbHeader.append(end);
        sbHeader.append(twoHyphens);
        sbHeader.append(boundaryString);
        sbHeader.append(end);

        OutputStream dos = connection.getOutputStream();
        dos.write(sbHeader.toString().getBytes());
        
    
	  
        File file = new File("D:\\tupiantest\\fanglin.png");
        dos.write(("Content-Disposition: form-data; name=\"photo\"; filename=\"" + file.getName() + "\"").getBytes());
        dos.write(end.getBytes());
        dos.write("Content-Type: application/octet-stream".getBytes());
        dos.write(end.getBytes());
        dos.write(end.getBytes());
        InputStream is = new FileInputStream(file);
        byte[] buffer = new byte[1024];

        int count = 0;
        long total = 0;
        while ((count = is.read(buffer)) != -1) {
            total += count;
            dos.write(buffer, 0, count);
        }
        is.close();
        dos.write(end.getBytes());
        StringBuilder sbEnd = new StringBuilder();
        sbEnd.append(twoHyphens);
        sbEnd.append(boundaryString);
        sbEnd.append(twoHyphens);
        sbEnd.append(end);

        dos.write(sbEnd.toString().getBytes());
        dos.flush();
        dos.close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String lines;
        StringBuffer sb = new StringBuffer("");
        while ((lines = reader.readLine()) != null) {
            lines = new String(lines.getBytes(), "utf-8");
            sb.append(lines);
        }
        //打印响应结果
        System.out.println(sb.toString());
   
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值