java - convert the uploaded file (xls) to xml -


i have been making app can convert uploaded file xml file. uploaded file xls file. have made xls converter xml apache-poi. have made upload , download functions too. when xls file being uploaded, should converted xml controller , saved in database (mysql), fails. app returns exception:

java.lang.classcastexception: org.springframework.web.multipart.commons.commonsmultipartfile cannot cast java.io.inputstream 

and controller:

@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 convert xls 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"; }  } 

maybe there's have controller. ideas, solution, or suggestion welcomed , appreciated.

best regards,

yunus

i think line giving error.

inputstream input = (inputstream) file; 

you cannot cast multipartfile object file directly inputstream. need use getinputstream() method of multipartfile, this:-

inputstream input = file.getinputstream(); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -