1.下載文件,將文件保存到本地。(只試用excel);
2.對(duì)文件的標(biāo)題進(jìn)行檢驗(yàn);
3.獲取導(dǎo)入的批次(取一個(gè)表的一個(gè)值,加1);
4.循環(huán)獲取文件某一個(gè)行,某一列的值,set到對(duì)象中;
5.檢驗(yàn)值的合法性;
6.循環(huán)保存到對(duì)象中。
7.用map將錯(cuò)誤的信息和正確的信息,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( "開(kāi)始導(dǎo)入規(guī)則文件:" + 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("規(guī)則文件導(dǎo)入成功:"+fileinputfilename); // out.println("規(guī)則文件導(dǎo)入成功:"+fileinputfilename); // }else{ // logger.info("規(guī)則文件導(dǎo)入失敗:"+fileinputfilename); // out.println("規(guī)則文件導(dǎo)入失敗,詳見(jiàn)日志:"+fileinputfilename); // } out.flush(); out.close(); return null ; } |
以上這篇java 文件上傳到讀取文件內(nèi)容的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/huoxingren0523/article/details/72366874