0
我一直在製作一個應用程序,可以將上傳的文件轉換爲xml文件。上傳的文件是xls文件。 我使用apache-poi將xls轉換器轉換爲xml。 我也做了上傳和下載功能。 當xls文件被上傳時,它應該被控制器轉換爲xml並保存在數據庫(Mysql)中,但是失敗。 該應用程序返回一個例外:將上傳的文件(xls)轉換爲xml
java.lang.ClassCastException: org.springframework.web.multipart.commons.CommonsMultipartFile cannot be cast to java.io.InputStream
這是控制器:
@Controller
public class FileController {
@Autowired
private FileDAO documentDao;
@RequestMapping("/index")
public String showDocument(Map<String, Object> map) {
try {
map.put("document", new File());
map.put("documentList", documentDao.list());
} catch (Exception e) {
e.printStackTrace();
}
return "documents";
}
@SuppressWarnings("deprecation")
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("document") File document,
@RequestParam("file") MultipartFile file) throws IOException {
System.out.println("Name:" + document.getName());
System.out.println("Desc:" + document.getDescription());
System.out.println("File:" + file.getName());
System.out.println("ContentType:" + file.getContentType());
/*
* start to convert xls to xml
* */
try {
InputStream input = (InputStream) file;
HSSFWorkbook workbook = new HSSFWorkbook(input);
HSSFSheet spreadsheet = workbook.getSheetAt(0);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.newDocument();
doc.setXmlStandalone(true);
Element spectraexchange = doc.createElementNS("http://www.lstelcom.com/Schema/SPECTRAexchange", "SPECTRAEXCHANGE");
spectraexchange.setAttribute("xmlns:xsd", "http://www.w3.org/2001/XMLSchema");
spectraexchange.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
spectraexchange.setAttribute("version", "2.4.28");
doc.appendChild(spectraexchange);
for(int i=1; i<=2; i++){
HSSFRow row = spreadsheet.getRow(i);
Element application = doc.createElement("APPLICATION");
spectraexchange.appendChild(application);
Element svid = doc.createElement("SV_SV_ID");
application.appendChild(svid);
svid.appendChild(doc.createTextNode("12"));
Element ssid = doc.createElement("SS_SS_ID");
application.appendChild(ssid);
ssid.appendChild(doc.createTextNode("140"));
Element address = doc.createElement("ADDRESS");//men-generate <ADDRESS>
application.appendChild(address);//men-generate </ADDRESS>
address.setAttribute("AD_TYPE", "L");
Element adspplus = doc.createElement("AD_SPPLUS_TYPE");//men-generate <AD_SPPLUS_TYPE>
address.appendChild(adspplus);//men-generate </AD_SPPLUS_TYPE>
adspplus.appendChild(doc.createTextNode("L"));//<AD_SPPLUS_TYPE>L<AD_SPPLUS_TYPE>
try {
Element adcompany = doc.createElement("AD_COMPANY");
address.appendChild(adcompany);
adcompany.appendChild(doc.createTextNode(row.getCell((short) 33).getStringCellValue()));
if(row.getCell((short) 33).getStringCellValue()==null){
continue;
}
} catch (Exception e) {
e.getMessage();
}
try {
Element adstreet = doc.createElement("AD_STREET");
address.appendChild(adstreet);
adstreet.appendChild(doc.createTextNode(row.getCell((short) 35).getStringCellValue()));
if(row.getCell((short) 35).getStringCellValue()==null){
continue;
}
} catch (Exception e) {
e.getMessage();
}
Element adcountry = doc.createElement("AD_COUNTRY");
address.appendChild(adcountry);
adcountry.appendChild(doc.createTextNode("INS"));
try {
Element adcity = doc.createElement("AD_CITY");
address.appendChild(adcity);
adcity.appendChild(doc.createTextNode(row.getCell((short) 36).getStringCellValue()));
if(row.getCell((short) 36).getStringCellValue()==null) {
continue;
}
} catch (Exception e) {
e.getMessage();
}
Element station = doc.createElement("STATION");
application.appendChild(station);
Element transmitter = doc.createElement("TRANSMITTER");
station.appendChild(transmitter);
try {
Element eqpname = doc.createElement("EQP_EQUIP_NAME");
transmitter.appendChild(eqpname);
eqpname.appendChild(doc.createTextNode(row.getCell((short) 2).getStringCellValue()));
if(row.getCell((short) 2).getStringCellValue()==null){
continue;
}
} catch (Exception e) {
e.getMessage();
}
Element eqptype = doc.createElement("EQP_TYPE_IS_APPROVED");
transmitter.appendChild(eqptype);
eqptype.appendChild(doc.createTextNode("1"));
try {
Element eqpmodel = doc.createElement("EQP_EQUIP_MODEL");
transmitter.appendChild(eqpmodel);
eqpmodel.appendChild(doc.createTextNode(row.getCell((short) 4).getStringCellValue()));
if(row.getCell((short) 4).getStringCellValue()==null){
continue;
}
} catch (Exception e) {
e.getMessage();
}
try {
Element eqpprod = doc.createElement("EQP_EQUIP_PROD");
transmitter.appendChild(eqpprod);
eqpprod.appendChild(doc.createTextNode(row.getCell((short) 6).getStringCellValue()));
if(row.getCell((short) 6).getStringCellValue()==null){
continue;
}
} catch (Exception e) {
e.getMessage();
}
}
TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer transformer = tfactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http:/xml.apache.org/xslt}indent-amount", "2");
transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);
//StreamResult result = new StreamResult("E://XlsToXml/standard_query02.xml");
transformer.transform(source, result);
/*
* end of convertion
* */
//----------------------------------//
Blob blob = Hibernate.createBlob(((MultipartFile) doc).getInputStream());
document.setFilename(((MultipartFile) doc).getOriginalFilename());
document.setContent(blob);
document.setContentType(((MultipartFile) doc).getContentType());
documentDao.save(document);
}catch (IOException e) {
System.out.println("IOException " + e.getMessage());
} catch (ParserConfigurationException e) {
System.out.println("ParserConfigurationException " + e.getMessage());
} catch (TransformerConfigurationException e) {
System.out.println("TransformerConfigurationException "+ e.getMessage());
} catch (TransformerException e) {
System.out.println("TransformerException " + e.getMessage());
}catch (Exception e) {
e.printStackTrace();
}
return "redirect:/index.html";
}
@RequestMapping("/download/{documentId}")
public String download(@PathVariable("documentId") Integer documentId,
HttpServletResponse response) {
File doc = documentDao.get(documentId);
try {
response.setHeader("Content-Disposition", "inline;filename=\""
+ doc.getFilename() + "\"");
OutputStream out = response.getOutputStream();
response.setContentType(doc.getContentType());
IOUtils.copy(doc.getContent().getBinaryStream(), out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@RequestMapping("/remove/{documentId}")
public String remove(@PathVariable("documentId") Integer documentId) {
documentDao.remove(documentId);
return "redirect:/index.html";
}
}
也許有些事我與控制器做。 任何想法,解決方案或建議都非常受歡迎和讚賞。
最好的問候,
尤努斯
它的工作原理。現在,控制器可以獲取文件並能夠將文件轉換爲xml。但是,該應用程序返回一個新的異常: 'java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.DocumentImpl無法轉換爲org.springframework.web.multipart.MultipartFile' 和文件不能保存到數據庫。 任何想法或建議? – yunus 2013-05-03 04:05:48
很高興能夠幫到你!:) – SudoRahul 2013-05-03 04:07:36