Path problem of file upload and download under Web

Source: Internet
Author: User
Tags uuid

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.