📄 multipartformoutputstream.java
字号:
package com.nari.pmos.arch;
import java.io.*;
import java.net.*;
public class MultiPartFormOutputStream {
public MultiPartFormOutputStream() {
out = null;
boundary = null;
}
public MultiPartFormOutputStream(OutputStream os, String boundary) {
out = null;
this.boundary = null;
if (os == null)
throw new IllegalArgumentException("Output stream is required.");
if (boundary == null || boundary.length() == 0) {
throw new IllegalArgumentException("Boundary stream is required.");
} else {
out = new DataOutputStream(os);
this.boundary = boundary;
return;
}
}
public void writeField(String name, boolean value) throws IOException {
writeField(name, (new Boolean(value)).toString());
}
public void writeField(String name, double value) throws IOException {
writeField(name, Double.toString(value));
}
public void writeField(String name, float value) throws IOException {
writeField(name, Float.toString(value));
}
public void writeField(String name, long value) throws IOException {
writeField(name, Long.toString(value));
}
public void writeField(String name, int value) throws IOException {
writeField(name, Integer.toString(value));
}
public void writeField(String name, short value) throws IOException {
writeField(name, Short.toString(value));
}
public void writeField(String name, char value) throws IOException {
writeField(name, (new Character(value)).toString());
}
public void writeField(String name, String value) throws IOException {
if (name == null) {
throw new IllegalArgumentException("Name cannot be null or empty.");
}
if (value == null) {
value = "";
}
out.writeBytes("--");
out.writeBytes(boundary);
out.writeBytes("\r\n");
out.writeBytes((new StringBuilder(
"Content-Disposition: form-data; name=\"")).append(name)
.append("\"").toString());
out.writeBytes("\r\n");
out.writeBytes("\r\n");
out.writeBytes(value);
out.writeBytes("\r\n");
out.flush();
}
public void writeFile(String name, String mimeType, File file)
throws IOException {
if (file == null)
throw new IllegalArgumentException("File cannot be null.");
if (!file.exists())
throw new IllegalArgumentException("File does not exist.");
if (file.isDirectory()) {
throw new IllegalArgumentException("File cannot be a directory.");
} else {
writeFile(name, mimeType, file,
((InputStream) (new FileInputStream(file))));
return;
}
}
public void writeFile(String name, String mimeType, File file,
InputStream is) throws IOException {
String fileName = file.getName();
if (is == null)
throw new IllegalArgumentException("Input stream cannot be null.");
if (fileName == null || fileName.length() == 0)
throw new IllegalArgumentException(
"File name cannot be null or empty.");
out.writeBytes("--");
out.writeBytes(boundary);
out.writeBytes("\r\n");
// 文件名要编码转换
out.writeBytes((new StringBuilder(
"Content-Disposition: form-data; name=\"")).append(name)
.append("\"; filename=\"").append(
new String(fileName.getBytes("GB2312"), "ISO-8859-1"))
.append("\"").toString());
out.writeBytes("\r\n");
if (mimeType != null) {
out.writeBytes((new StringBuilder("Content-Type: ")).append(
mimeType).toString());
out.writeBytes("\r\n");
}
out.writeBytes("\r\n");
byte data[] = new byte[1024];
for (int r = 0; (r = is.read(data, 0, data.length)) != -1;)
out.write(data, 0, r);
try {
is.close();
} catch (Exception exception) {
}
out.writeBytes("\r\n");
out.flush();
}
public void close() throws IOException {
out.writeBytes("--");
out.writeBytes(boundary);
out.writeBytes("--");
out.writeBytes("\r\n");
out.flush();
out.close();
}
public String getBoundary() {
return boundary;
}
public static URLConnection createConnection(URL url) throws IOException {
URLConnection urlConn = url.openConnection();
if (urlConn instanceof HttpURLConnection) {
HttpURLConnection httpConn = (HttpURLConnection) urlConn;
httpConn.setRequestMethod("POST");
}
urlConn.setDoInput(true);
urlConn.setDoOutput(true);
urlConn.setUseCaches(false);
urlConn.setDefaultUseCaches(false);
return urlConn;
}
public static String createBoundary() {
return (new StringBuilder("--------------------")).append(
Long.toString(System.currentTimeMillis(), 16)).toString();
}
public static String getContentType(String boundary) {
return (new StringBuilder("multipart/form-data; boundary=")).append(
boundary).toString();
}
public static void main(String args[]) {
try {
(new MultiPartFormOutputStream()).upLoadFile("10000", "你好",
"hello", "1000:1001", null,
"http://localhost:7001/pmos2000/pmos2000/fileupload");
} catch (IOException e) {
e.printStackTrace();
}
}
public void upLoadFile(String participant_Id, String pub_Info,
String topic, String participant_List, String fileName,
String url_str) throws IOException {
URL url = new URL(url_str);
String boundary = createBoundary();
URLConnection urlConn = createConnection(url);
urlConn.setRequestProperty("Accept", "*/*");
urlConn.setRequestProperty("Content-Type", getContentType(boundary));
urlConn.setRequestProperty("Connection", "Keep-Alive");
urlConn.setRequestProperty("Cache-Control", "no-cache");
MultiPartFormOutputStream out = new MultiPartFormOutputStream(urlConn
.getOutputStream(), boundary);
out.writeField("PARTICIPANT_ID", participant_Id);
// 转换编码方式
out.writeField("PUB_INFO", FormatToUnicode.GB2312ToUnicode(pub_Info));
out.writeField("TOPIC", FormatToUnicode.GB2312ToUnicode(topic));
out.writeField("PARTICIPANT_LIST", participant_List);
out.writeField("ATTACHMENT", fileName == null ? "NO_ATTACHMENT"
: "ATTACHMENT");
if (fileName != null) {
out.writeFile("myFile", "text/plain", new File(fileName));
}
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(urlConn
.getInputStream()));
in.close();
}
private static final String NEWLINE = "\r\n";
private static final String PREFIX = "--";
private DataOutputStream out;
private String boundary;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -