~~根据输入流获取文本内容
public StringBuilder getStreamContent(InputStream inputStream) {
StringBuilder stringBuilder =new StringBuilder();
BufferedReader bufferedReader =null;
try {
if (inputStream !=null) {
bufferedReader =new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
char[]charBuffer =new char[128];
int bytesRead = -1;
while ((bytesRead = bufferedReader.read(charBuffer)) >0) {
stringBuilder.append(charBuffer,0, bytesRead);
}
}else {
stringBuilder.append("");
}
}catch (IOException ex) {
}finally {
if (inputStream !=null) {
try {
inputStream.close();
}catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedReader !=null) {
try {
bufferedReader.close();
}catch (IOException e) {
e.printStackTrace();
}
}
}
return stringBuilder;
}
~~获取输入文件流的内容
public StringBuilder getFieldContent(HttpServletRequest request,String key)throws Exception {
StringBuilder xmlContent =null;
CommonsMultipartResolver multipartResolver =new CommonsMultipartResolver(
request.getSession().getServletContext());
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator iterator =multiRequest.getFileNames();
while (iterator.hasNext()) {
MultipartFile multipartFile =multiRequest.getFile(iterator.next().toString());
if (multipartFile !=null) {
String fileName =multipartFile.getOriginalFilename();
CommonsMultipartFile cf = (CommonsMultipartFile)multipartFile;
String form_name =cf.getFileItem().getFieldName();
if (form_name.equals(key)) {
xmlContent =impComSer.getStreamContent(multipartFile.getInputStream());
}
}
}
}
return xmlContent;
}
~~xml转json
System.out.println(xml);
InputStream is =new ByteArrayInputStream(xml.getBytes("UTF-8"));
XMLSerializer xmlSerializer =new XMLSerializer();
JSON json =xmlSerializer.readFromStream(is);
System.out.println(json);