JAVA上传文件

ServletInputStream in = request.getInputStream();
String charset = request.getCharacterEncoding();
String line = null;
byte[] buffer = new byte[256];
StringBuffer sb = new StringBuffer();
while((line = readLine(buffer, in, charset)) != null){
if(line.startsWith("Content-Disposition: form-data")){
int index = line.lastIndexOf("\\");
String fileName = line.substring(index+1, line.length()-3);
logger.info("fileName:" + fileName);
}else if(line.startsWith("-----------") || line.startsWith("Content-Type") || line.startsWith("<?xml")){
continue;
}else{
String content = line.replaceAll("\n", "");
if(!content.startsWith("?")){
sb.append(content);
}
}
}

private String readLine(byte[] Linebyte, ServletInputStream servletinputstream, String charset){
String bufferTemp = null;
try {
int len = servletinputstream.readLine(Linebyte, 0, Linebyte.length);
if (len == -1) {
return null;
}

if (charset == null) {
bufferTemp = new String(Linebyte, 0, len);
}else {
bufferTemp = new String(Linebyte, 0, len, charset);
}
}catch(Exception _ex) {
logger.error(_ex.getMessage(), _ex);
}
return bufferTemp;
}

你可能感兴趣的:(java)