@RequestMapping ("Download") Public voidExportword (httpservletrequest request, httpservletresponse response)throwsException {User User=Appcontext.getloginuser (); Student Student=Studentsvc.findbyuserid (User.getid ()); Try { //Word contentString content= "; byteB[] = content.getbytes ("Utf-8");//here is the need to set the encoding, or export Chinese will be garbled. Bytearrayinputstream Bais =NewBytearrayinputstream (b);//wrapping a byte array into a stream /** Key Place * Generate Word format*/Poifsfilesystem poifs=NewPoifsfilesystem (); DirectoryEntry Directory=Poifs.getroot (); Documententry Documententry= Directory.createdocument ("Worddocument", Bais); //Output FileString filename= "Internship examination and appraisal form"; Request.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Application/msword");//Export Word formatResponse.AddHeader ("Content-disposition", "attachment;filename=" +NewString ((FileName + ". Doc"). GetBytes (),"Iso-8859-1")); OutputStream ostream=Response.getoutputstream (); Poifs.writefilesystem (ostream); Bais.close (); Ostream.close (); }Catch(Exception e) {apputils.logerror ("Export Error:%s", E.getmessage ()); } }
Java exports HTML to Word format