Engineering structure
1. Generate a file to the specified folder
// This .setfilename (string.valueof (System.currenttimemillis ())); String Path = Servletactioncontext.getservletcontext (). Getrealpath ("/template/wordexporttemplate" // full path name under Project String filepath = path + "\ \" + FileName + ". Doc" // write to file Writer out = new bufferedwriter (new OutputStreamWriter (new fileoutputstream (FilePath, true ), "Utf-8"), 10240 "");
2. Output the file under the specified file to the client (the output of the file generated above)
Public throws Exception { create (); = Servletactioncontext.getservletcontext (). Getrealpath ("/template/wordexporttemplate"); = path + "\ \" + FileName + ". doc" ; // the full path name New File (filepath); // returns only one input stream return fileutils.openinputstream (file); }
3. Save the file uploaded by the client to the specified folder under a specific name:
PublicString Execute ()throwsexception{ServletContext ServletContext=Servletactioncontext.getservletcontext ();//Filenamefilename is the name of the file when it is uploaded, or you can define a new name with the UUID yourself .String dir = Servletcontext.getrealpath ("/template/excelimporttemplate/" +filenamefilename); System.out.println (dir);//file output stream, written to DIR-specified directory with first nameFileOutputStream OutputStream =NewFileOutputStream (dir);//Open the input stream of the uploaded fileFileInputStream InputStream =NewFileInputStream (fileName); byte[] buffer =New byte[1024]; intLen = 0;//reads a maximum of b.length bytes of data from this input stream into a byte array. The total number of bytes read into the buffer, or 1 if there is no more data because the end of the file has been reached. while(len = inputstream.read (buffer))!=-1){//writes Len bytes from offset off in the specified byte array to this file output stream. Outputstream.write (buffer, 0, Len); } inputstream.close (); Outputstream.close (); returnSUCCESS; }
4. Read the file with the specified name under the specified folder:
ServletContext ServletContext = Servletactioncontext.getservletcontext ();
Filenamefilename is the name of the file when it is uploaded, or you can define a new name with the UUID yourself.
String dir = Servletcontext.getrealpath ("/template/excelimporttemplate/" +filenamefilename);
New File (dir);
FileInputStream Openinputstream = fileutils.openinputstream (file);
Hssfworkbook workbook = new Hssfworkbook (openinputstream);
5. FileInputStream FileOutputStream is a class. Can read and write directly.
Construction method
For example:
Public throws Exception { create (); = Servletactioncontext.getservletcontext (). Getrealpath ("/template/excelexporttemplate"); = path + "\ \" + FileName + ". xls" ; New File (filepath); // returns only one input stream return fileutils.openinputstream (file); }
String Path = Servletactioncontext.getservletcontext (). Getrealpath ("/template"); File Tempfile = new file (path, templatename + ". xls"); Tempfile.delete (); Tempfile.createnewfile ();
= Fileutils.openoutputstream (tempfile);
1 ServletContext servletcontext = servletactioncontext.getservletcontext (); 2 // Filenamefilename is the name of the file when it is uploaded, or you can define a new name with the UUID yourself . 3 String dir = Servletcontext.getrealpath ("/template/excelimporttemplate/" +filename); 4 FileOutputStream OutputStream = new FileOutputStream (dir);
Path problem of file upload and download under Web