50,504
社区成员
发帖
与我相关
我的任务
分享
private static final String BOUNDARYSTR = "XMKSSS***********";
private static final String BOUNDARY = "--" + BOUNDARYSTR + "\r\n";
private static void sendPost(String urlString, String filePath) {
HttpURLConnection urlConnection = null;
FileInputStream fi = null;
OutputStream o = null;
try {
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("fileName", "文件.xls");
// parameters.put("fileName", filePath);
Map<String, String> propertys = getHeader();
URL url;
url = new URL(urlString);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-type", "multipart/form-data;boundary=" + BOUNDARYSTR);
if (propertys != null)
for (String key : propertys.keySet()) {
urlConnection.addRequestProperty(key, propertys.get(key));
}
urlConnection.connect();
BufferedOutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
if (parameters != null) {
StringBuffer param = new StringBuffer();
for (String key : parameters.keySet()) {
param.append(BOUNDARY);
param.append("Content-Disposition:form-data;name=\"");
param.append(key);
param.append("\"\r\n\r\n");
param.append(parameters.get(key));
param.append("\r\n");
}
out.write(param.toString().getBytes());
}
File file = new File(filePath);
out.write(BOUNDARY.getBytes());
StringBuilder filenamesb = new StringBuilder();
filenamesb.append("Content-Disposition:form-data;Content-Type:application/octet-stream;name=\"uploadfile\";filename=\"");
filenamesb.append(file.getName() + "\"\r\n\r\n");
out.write(filenamesb.toString().getBytes());
FileInputStream fis = new FileInputStream(file);
byte[] buffer = new byte[8192]; // 8k
int count = 0;
// 读取文件
while ((count = fis.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
out.write("\r\n\r\n".getBytes());
fis.close();
out.write(("--" + BOUNDARYSTR + "--\r\n").getBytes());
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fi != null) {
fi.close();
}
if (o != null) {
o.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
早期的代码,你自己调试下
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> heads = request.getHeaderNames();
System.out.println("request.getHeaderNames()");
while (heads.hasMoreElements()) {
String header = heads.nextElement();
System.out.println(header + "=" + request.getHeader(header));
}
Part part = request.getPart("filename");
Collection<String> headerNames = part.getHeaderNames();
System.out.println("part.getHeaderNames()");
for (String header : headerNames) {
System.out.println(header + "=" + part.getHeader(header));
}
InputStream is = part.getInputStream();
String filename = new String(getFilename(part).getBytes(), "UTF-8");
File file = new File("D:\\fileupload");
if (!file.exists() || !file.isDirectory()) {
file.mkdir();
}
FileOutputStream fos = new FileOutputStream(new File(file + File.separator + filename));
byte[] buf = new byte[1024];
while (is.read(buf) != -1) {
fos.write(buf);
}
fos.flush();
fos.close();
is.close();
PrintWriter out = response.getWriter();
out.println("file upload success");
}
public String getFilename(Part part) {
String contentDispositionHeader = part.getHeader("content-disposition");
String[] elements = contentDispositionHeader.split(";");
for (String element : elements) {
if (element.trim().startsWith("filename")) {
return element.substring(element.indexOf('=') + 1).trim().replace("\"", "");
}
}
return null;
}