java 文件上传到读取文件内容的实例
作者:飘在北季的校园- 时间:2023-11-09 22:00:27
1.下载文件,将文件保存到本地。(只试用excel);
2.对文件的标题进行检验;
3.获取导入的批次(取一个表的一个值,加1);
4.循环获取文件某一个行,某一列的值,set到对象中;
5.检验值的合法性;
6.循环保存到对象中。
7.用map将错误的信息和正确的信息,JSONObject.fromObject(map);
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;
}
来源:https://blog.csdn.net/huoxingren0523/article/details/72366874
标签:java,文件,上传
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JavaSE static final及abstract修饰符实例解析
2021-07-01 10:54:54
![](https://img.aspxhome.com/file/2023/9/62129_0s.png)
IDEA导入Eclipse项目的方法步骤(图文教程)
2023-07-17 09:45:09
![](https://img.aspxhome.com/file/2023/8/78358_0s.png)
Maven 错误找不到符号的解决方法
2021-07-19 09:03:02
![](https://img.aspxhome.com/file/2023/7/62387_0s.png)
C#中list用法实例
2022-07-02 13:57:00
android 版本检测 Android程序的版本检测与更新实现介绍
2022-12-02 11:27:41
![](https://img.aspxhome.com/file/2023/7/109157_0s.png)
Android Bitmap和Drawable的对比
2021-11-16 06:03:41
C#关于System.Collections空间详解
2022-04-12 16:04:06
SpringBoot实现分页功能
2021-11-07 12:33:16
c#多线程之线程基础
2022-10-06 20:34:44
详解MyBatis直接执行SQL查询及数据批量插入
2021-12-02 17:52:08
springboot post接口接受json时,转换为对象时,属性都为null的解决
2023-06-17 15:24:23
![](https://img.aspxhome.com/file/2023/3/57313_0s.png)
通过C#实现自动售货机接口
2023-12-16 00:15:36
Spring Boot FeignClient 如何捕获业务异常信息
2022-01-26 11:57:41
Unity shader实现百叶窗特效
2023-06-05 00:05:49
![](https://img.aspxhome.com/file/2023/0/104310_0s.png)
android动态设置app当前运行语言的方法
2022-02-21 01:33:37
![](https://img.aspxhome.com/file/2023/1/100611_0s.jpg)
Android实现语音播放与录音功能
2022-01-21 15:39:09
Java concurrency集合之 CopyOnWriteArrayList_动力节点Java学院整理
2022-10-27 05:03:36
![](https://img.aspxhome.com/file/2023/8/104048_0s.png)
java转换字符串编码格式的方法
2022-02-12 04:30:24
![](https://img.aspxhome.com/file/2023/0/129200_0s.png)
Android开发手册shape属性和子属性使用说明
2023-11-02 09:44:43
IDEA集成MyBatis Generator插件的使用
2023-08-12 00:28:47
![](https://img.aspxhome.com/file/2023/6/57896_0s.png)