对发送文件的格式要求进行了测试,有需要的同学可以参考一下。
package controller;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/** 以下代码非常重要!!!千万不要动!!复制粘贴使用!!!
*
*
*/
public class POST_MultiData {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/test/POST");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 构建请求头,请求头是随便构建的,任何的字符串都可以。一般来说,以 - 为开头。
String boundary = "-------------" + System.currentTimeMillis();
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Charset", "UTF-8");
conn.connect();
OutputStream outputStream = conn.getOutputStream();
StringBuilder sb = new StringBuilder();
String newLine = "\r\n";
String boundaryPrefix = "--";
sb.append(newLine); //此处注意,作为开头,是可以,将newline省去的,也就是不以 "\r\n"为开头,也可以解析。
sb.append(boundaryPrefix); //此处注意,作为boundaryPrefix 的--, 是不可以省略的,且 boundaryPrefix,必须为 -- 。
sb.append(boundary); //此处注意,作为multipart/form-data的格式,是以boundary为开头,之后为内容的。也就是在每一段内容的开头,都是boundary,此处boundary不可以省略。
sb.append(newLine); //此处注意,boundary之后要有一个回车换行,也就是“\r\n”,因为此处的stringbuild为用代码组织的形式,并不是实际的string的样子,因此常常会马虎而遗忘调一个回车换行,如果确实,解析就会失败。
sb.append("Content-Disposition:form-data;name=\"to\""); //此处规定传输的内容,其中,Content-Dispositon:form-data; 是固定的,name规定的是健的名字,如果是文件还可以跟文件名。网上的参考很多。
sb.append(newLine); //此处注意,Content-Dispostion之后必须要有一个回车换行,否则是无法解析的。且会返回500,服务器报错。
sb.append(newLine); //此处注意,在以上的格式声明后,还是需要一个回车换行的,否则还是报错。也就是,内容键的声明和内容之间要有一个换行。这个是必须的,否则会500服务器报错。
sb.append(newLine);
sb.append(newLine); //此处的回车换行是可以加几个都可以的。但是,必须要至少有一个,且之后的回车都会被认为是内容中的!!!也就是至少要有一个空行!!
sb.append("www");
sb.append(newLine); //此处的换行也是不允许省略的,否则是报错,这个很好理解。因为内容要以回车换行来标注分隔。
sb.append(boundaryPrefix); //不可省略,否则报错。
sb.append(boundary); //此处为结尾的boundary,不可省略。
outputStream.write(sb.toString().getBytes());
outputStream.write(newLine.getBytes()); //此处的newline 是不可以多的,必须一个没有,也就是在boundary之后,紧跟着就是Content-Disposition的声明。
//outputStream.write(newLine.getBytes()); 该回车换行不可以存在,否则该记录无效,但是下一个记录是有效的,也就是,程序会以boundary为判断点。
outputStream.write(("Content-Disposition:form-data;name=\"file\";" + "fileName=" + "\"textfile.txt\"").getBytes());
outputStream.write(newLine.getBytes());
//outputStream.write(newLine.getBytes()); 此处的空行不可以有,只可以有一个回车作为换行,不然,程序会把一下的格式声明字符判定为文件里的内容。也就是,multipart/form-data 是严格格式的。
//outputStream.write(newLine.getBytes());
outputStream.write("Content-Type: application/octet-stream".getBytes());
outputStream.write(newLine.getBytes());
//outputStream.write(newLine.getBytes()); 此处的空行也是不要有的,不然也会把下面的格式声明当作为文件内容进行处理。
//outputStream.write(newLine.getBytes());
outputStream.write("Content-Transfer-Encoding: binary".getBytes());
outputStream.write(newLine.getBytes()); //此处的空行是必须有一个,一个以下会被认作为文件中的内容。如果没有,会出现各种不可预测的问题,如本文件可以解析,但是本文件之后的文件不可以解析。情况不可预测。
outputStream.write(newLine.getBytes());
BufferedInputStream in = new BufferedInputStream( new FileInputStream( "testfile.txt"));
System.out.println( "Available bytes:" + in.available());
byte[] temp = new byte[100];
int size = 0;
while ((size = in.read(temp)) != -1) {
outputStream.write(temp);
}
in.close();
outputStream.write(newLine.getBytes()); //此处换行作为文件结束的标志。
outputStream.write(boundaryPrefix.getBytes());
outputStream.write(boundary.getBytes());
outputStream.write(newLine.getBytes());
outputStream.write(("Content-Disposition:form-data;name=\"file\";" + "fileName=" + "\"textfile1.txt\"").getBytes());
outputStream.write(newLine.getBytes());
outputStream.write("Content-Type: application/octet-stream".getBytes());
outputStream.write(newLine.getBytes());
outputStream.write("Content-Transfer-Encoding: binary".getBytes());
outputStream.write(newLine.getBytes());
outputStream.write(newLine.getBytes());
in = new BufferedInputStream( new FileInputStream( "testfile.txt"));
System.out.println( "Available bytes:" + in.available());
temp = new byte[100];
size = 0;
while ((size = in.read(temp)) != -1) {
outputStream.write(temp);
}
in.close();
outputStream.write(newLine.getBytes());
outputStream.write(boundaryPrefix.getBytes());
outputStream.write(boundary.getBytes());
//outputStream.write(boundaryPrefix.getBytes()); //此处的结尾也是有格式要求,如果在结尾出现了回车换行,那么之前必须有boundaryPrefix,如果没有回车换行,也不需要boundaryPrefix
//outputStream.write(newLine.getBytes()); //且,回车换行至多一个。也就是,不允许出现空行。boundary之后是不可以接回车作为结尾的。但是boundaryPrefix可以接回车作为结尾。
System.out.println(outputStream);
outputStream.flush();
System.out.println("done");
System.out.println(conn.getResponseCode());
}catch (Exception e){
System.out.println("something wrong");
System.out.println(e);
}
}
}