我的代码:(可以不用看)
String path = getClass().getResource("").getPath();
int index = path.lastIndexOf("WEB-INF");
path = path.substring(1, index);
path = path + "upload/";
String contentType = request.getContentType();
int contentTypeLength = request.getContentLength();
int boundaryIndex = contentType.indexOf("boundary=");
String boundary = contentType.substring(boundaryIndex + 9);
int boundaryStrLength = boundary.length(); ServletInputStream serIn = request.getInputStream();
int line;
byte[] buffer = new byte[4096];
while ((line = serIn.readLine(buffer, 0, contentTypeLength)) != -1) {
if (buffer[0] == 13 | buffer[0] == 10)
break;
}
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(
contentTypeLength);
int len;
while ((len = serIn.read(buffer)) >= 0) {
byteOut.write(buffer, 0, len);
}
byte[] outBytes = byteOut.toByteArray();
BufferedOutputStream fileOut = new BufferedOutputStream(
new FileOutputStream(path + "uploaded_file"));
fileOut.write(outBytes, 0, outBytes.length - boundaryStrLength - 8);
fileOut.flush();
fileOut.close();