java 读取上传文件内容_java 文件上传到读取文件内容的实例

java 文件上传到读取文件内容的实例

发布于 2020-4-1|

复制链接

分享一篇关于java 文件上传到读取文件内容的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小妖过来看看吧

1.下载文件,将文件保存到本地。(只试用excel);2.对文件的标题进行检验;3.获取导入的批次(取一个表的一个值,加1);4.循环获取文件某一个行,某一列的值,set到对象中;5.检验值的合法性;6.循环保存到对象中。7.用map将错误的信息和正确的信息,JSONObject.fromObject(map);

```java

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 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;

}

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值