80,359
社区成员
发帖
与我相关
我的任务
分享
/**
* 直接通过HTTP协议提交数据到服务器,实现表单提交功能
* @param actionUrl 上传路径
* @param file 上传文件
*/
public static String post(String actionUrl,FormFile[] files) {
try {
String BOUNDARY = "----WebKitFormBoundaryBnnaepxTxlPONaHC"; //数据分隔线
String MULTIPART_FORM_DATA = "multipart/form-data";
URL url = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);//允许输入
conn.setDoOutput(true);//允许输出
conn.setUseCaches(false);//不使用Cache
conn.setRequestMethod("POST");
conn.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Length", "640619");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FORM_DATA + "; boundary=" + BOUNDARY);
conn.setRequestProperty("user-agent",
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36");
DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
//上传的文件部分,格式请参考文章
for(FormFile file : files){
StringBuilder split = new StringBuilder();
split.append("-");
split.append(BOUNDARY);
split.append("/r/n");
split.append("Content-Disposition: form-data; name=\""+ file.getFormname()+"\"; filename=\""+ file.getFilname() + "\"/r/n");
split.append("Content-Type: "+ file.getContentType()+"/r/n/r/n");
System.out.println(split.toString());
outStream.write(split.toString().getBytes());
outStream.write(file.getData(), 0, file.getData().length);
outStream.write("/r/n".getBytes());
}
byte[] end_data = ("--" + BOUNDARY + "--/r/n").getBytes();//数据结束标志
outStream.write(end_data);
outStream.flush();
int cah = conn.getResponseCode();
if (cah != 200) throw new RuntimeException("请求url失败");
InputStream is = conn.getInputStream();
int ch;
StringBuilder b = new StringBuilder();
while( (ch = is.read()) != -1 ){
b.append((char)ch);
}
outStream.close();
conn.disconnect();
return b.toString();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
class FormFile {
/* 上传文件的数据 */
private byte[] data;
/* 文件名称 */
private String filname;
/* 表单字段名称*/
private String formname;
/* 内容类型 */
private String contentType = "application/octet-stream"; //需要查阅相关的资料
public FormFile(byte[] data, String formname,String filname,String contentType) {
this.data = data;
this.filname = filname;
this.formname = formname;
if(contentType!=null) this.contentType = contentType;
}
public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
}
public String getFilname() {
return filname;
}
public void setFilname(String filname) {
this.filname = filname;
}
public String getFormname() {
return formname;
}
public void setFormname(String formname) {
this.formname = formname;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}