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());
}
}
java模拟表单上传图片简单明了版本个人独创
最新推荐文章于 2024-08-03 09:38:45 发布