首页 > 代码库 > 上传文件实现方法

上传文件实现方法

public static String formUpload(String urlStr, Map<String, String> textMap, Map<String, String> fileMap, String uri) {
String res = "";
HttpURLConnection conn = null;
String BOUNDARY = "---------------------------123821742118716"; // boundary就是request头和上传文件内容的分隔符
try {
URL url = new URL(urlStr);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

OutputStream out = new DataOutputStream(conn.getOutputStream());
// text
if (textMap != null) {
StringBuilder strBuilder = new StringBuilder();
for (Object o : textMap.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String inputName = (String) entry.getKey();
String inputValue = http://www.mamicode.com/(String) entry.getValue();
if (inputValue =http://www.mamicode.com/= null) {
continue;
}
strBuilder.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuilder.append("Content-Disposition: form-data; name=\"").append(inputName).append("\"\r\n\r\n");
strBuilder.append(inputValue);
}
out.write(strBuilder.toString().getBytes());
}

// file
if (fileMap != null) {
for (Object o : fileMap.entrySet()) {
Map.Entry entry = (Map.Entry) o;
String inputName = (String) entry.getKey();
String inputValue = http://www.mamicode.com/(String) entry.getValue();
if (inputValue =http://www.mamicode.com/= null) {
continue;
}

InputStream inputStream = new URL(uri).openStream();
File file = new File(inputValue);
String filename = file.getName();
String contentType = new MimetypesFileTypeMap().getContentType(file);
if (filename.endsWith(".png")) {
contentType = "image/png";
}
if (contentType == null || contentType.equals("")) {
contentType = "application/octet-stream";
}

StringBuilder strBuilder = new StringBuilder();
strBuilder.append("\r\n").append("--").append(BOUNDARY).append("\r\n");
strBuilder.append("Content-Disposition: form-data; name=\"").append(inputName).append("\"; filename=\"").append(filename).append("\"\r\n");
strBuilder.append("Content-Type:").append(contentType).append("\r\n\r\n");

out.write(strBuilder.toString().getBytes());

DataInputStream in = new DataInputStream(inputStream);
int bytes;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
}
}

byte[] endData = http://www.mamicode.com/("\r\n--" + BOUNDARY + "--\r\n").getBytes();
out.write(endData);
out.flush();
out.close();

// 读取返回数据
StringBuilder strBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
strBuilder.append(line).append("\n");
}
res = strBuilder.toString();
reader.close();
} catch (Exception e) {
System.out.println("发送POST请求出错。" + urlStr);
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return res;
}

上传文件实现方法