由于服务文件服务器的优化调整,导致文件上传不得不进行修改。
这是修改后的代码:
/**
* 获取文件类型
* @param fileUrl
* @return
* @throws java.io.IOException
*/
public static String getMimeType(String fileUrl)
throws java.io.IOException
{
FileNameMap fileNameMap = URLConnection.getFileNameMap();
String type = fileNameMap.getContentTypeFor(fileUrl);
return type;
}
/* 上传文件至Server,uploadUrl:接收文件的处理页面 */
public static String uploadFile(String uploadUrl, String filePath) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try {
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setReadTimeout(30000);
// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
// 允许输入输出流
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// 使用POST方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
httpURLConnection.getOutputStream());
StringBuilder sb = new StringBuilder();
/**
* 上传文件的头
*/
sb.append("--" + boundary + end);
sb.append("Content-Disposition: form-data; name=\"" + "uplaodFile"
+ "\"; filename=\"" + fileName + "\"" + end);
// 如果服务器端有文件类型的校验,必须明确指定ContentType
sb.append("Content-Type: " + getMimeType(filePath) + end);
sb.append("\r\n");
byte[] headerInfo = sb.toString().getBytes("UTF-8");
dos.write(headerInfo);
FileInputStream fis = new FileInputStream(filePath);
byte[] buffer = new byte[1024]; // 1k
int count = 0;
// 读取文件
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
dos.close();
is.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
修改前的代码基本跟网上百度出来的一样:
/* 上传文件至Server,uploadUrl:接收文件的处理页面 */
public static String uploadFile(String uploadUrl, String filePath) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "******";
try {
URL url = new URL(uploadUrl);
HttpURLConnection httpURLConnection = (HttpURLConnection) url
.openConnection();
// 设置每次传输的流大小,可以有效防止手机因为内存不足崩溃
// 此方法用于在预先不知道内容长度时启用没有进行内部缓冲的 HTTP 请求正文的流。
httpURLConnection.setChunkedStreamingMode(128 * 1024);// 128K
// 允许输入输出流
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
// 使用POST方法
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type",
"multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(
httpURLConnection.getOutputStream());
dos.writeBytes(twoHyphens + boundary + end);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\""
+ filePath.substring(filePath.lastIndexOf("/") + 1)
+ "\""
+ end);
dos.writeBytes(end);
FileInputStream fis = new FileInputStream(filePath);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
// 读取文件
while ((count = fis.read(buffer)) != -1) {
dos.write(buffer, 0, count);
}
fis.close();
dos.writeBytes(end);
dos.writeBytes(twoHyphens + boundary + twoHyphens + end);
dos.flush();
InputStream is = httpURLConnection.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
String result = br.readLine();
dos.close();
is.close();
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
咋一看还差不多,其实还是有点差别的。