Java files are uploaded to the server

Source: Internet
Author: User

Recent projects have used the file from local to server functionality. In fact, in order to solve the current browser does not support getting local file full path. Had to think of the fixed directory uploaded to the server, so that the project to obtain files, so that the program supports Excel batch import data.

<form method= "POST" enctype= "Multipart/form-data" action= "in the foreground interface. /manage/excelimport.do "> Please select File: <input type=" file "name=" Excelfile "> <input type=" Submit "value=" Import "Onclic k= "return Impexcel ();" /></form>action get data from the foreground and save/** * Excel import File * @return * @throws IOException */@RequestMapping ("/usermanager/ Excelimport.do ") public string Excelimport (string filepath,multipartfile excelfile,httpservletrequest request) throws Ioexception{log.info ("<<<<<<action:{} method:{} start>>>>>>", "Usermanager", " Excelimport "); if (excelfile! = null) {String filename=excelfile.getoriginalfilename (); String A=request.getrealpath ("u/cms/www/201509"); Savefilefrominputstream (Excelfile.getinputstream (), Request.getrealpath ("u/cms/www/201509"), filename);// Save the path to the server}log.info ("<<<<<<action:{} method:{} end>>>>>>", "Usermanager", " Excelimport "); return" ";} /** * Convert Multipartfile to file and save to a place on the server */public void SavefilefromiNputstream (InputStream stream,string path,string savefile) throws ioexception{FileOutputStream fs=new FileO        Utputstream (path + "/" + SaveFile);        System.out.println ("------------" +path + "/" + SaveFile);        byte[] buffer =new byte[1024*1024];        int bytesum = 0;        int byteread = 0;            while ((Byteread=stream.read (buffer))!=-1) {bytesum+=byteread;            Fs.write (Buffer,0,byteread);        Fs.flush ();        } fs.close (); Stream.Close ();}

Upload Java files to the server

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.