1.下載文件,將文件保存到本地。(只試用excel);
2.對文件的標題進行檢驗;
3.獲取導入的批次(取一個表的一個值,加1);
4.循環獲取文件某一個行,某一列的值,set到對象中;
5.檢驗值的合法性;
6.循環保存到對象中。
7.用map將錯誤的信息和正確的信息,jsonobject.fromobject(map);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
public string uploadfile() throws exception { logger.info( "開始導入規則文件:" + fileinputfilename); inputstream is = new fileinputstream(fileinput); string uploadpath = servletactioncontext.getservletcontext() .getrealpath( "/upload" ); system.out.println(uploadpath); file targetfile = new file(uploadpath, this .getfileinputfilename()); outputstream os = new fileoutputstream(targetfile); byte [] buffer = new byte [ 1024 ]; int length = 0 ; while ((length = is.read(buffer)) > 0 ) { os.write(buffer, 0 , length); } is.close(); os.close(); map<string, object> map = personalkpimonitormanualruleservice .importrules(targetfile); httpservletresponse response = servletactioncontext.getresponse(); response.setcontenttype( "text/html;charset=utf-8" ); printwriter out = response.getwriter(); out.println(jsonobject.fromobject(map)); // if(flag==true){ // logger.info("規則文件導入成功:"+fileinputfilename); // out.println("規則文件導入成功:"+fileinputfilename); // }else{ // logger.info("規則文件導入失敗:"+fileinputfilename); // out.println("規則文件導入失敗,詳見日志:"+fileinputfilename); // } out.flush(); out.close(); return null ; } |
以上這篇java 文件上傳到讀取文件內容的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/huoxingren0523/article/details/72366874