1,spring配置文件添加文件上传配置

<!-- 上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 1GB -->
<property name="maxUploadSize" value="" />
<property name="defaultEncoding" value="utf-8"/>
</bean>

2,html

 <form method="post" accept-charset="UTF-8"  th:action="${#httpServletRequest.contextPath+'/upload'}" id="multipartform" enctype="multipart/form-data"  class="form-search" role="form">
<input type="file" name="file" size="" id="file" /><button type="submit" id="multipart" class="btn">上传</button>
</form>

3,controller

     @RequestMapping(value = "/upload",method= RequestMethod.POST)
@ResponseBody
public String upload(HttpServletRequest request,HttpServletResponse response,@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return ResponseJSON.instance().setStatus(false).addAlertMessage("请选择导入文件!").toJSON();
}
String filename = file.getOriginalFilename();
int index = filename.indexOf(".");
String file_suffix = filename.substring(index, filename.length());
if (!file_suffix.equals(".txt")) {
return ResponseJSON.instance().setStatus(false).addAlertMessage("请选择txt格式文件!").toJSON();
}
try {
BufferedReader reader = null;
String usernameString = null;
InputStreamReader isr = null;
int line = ;
try {
isr = new InputStreamReader(file.getInputStream(), "utf-8");// 考虑到编码格式
reader = new BufferedReader(isr);
while ((usernameString = reader.readLine()) != null) {
system.out.println(usernameString);
line++;
} } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
logger.error("读取文件失败!", e);
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("读取文件失败!", e);
} finally {
reader.close();
isr.close();
in.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
logger.error("上传文件失败", e);
return ResponseJSON.instance().setStatus(false).addAlertMessage("上传文件失败").toJSON();
}
return ResponseJSON.instance().setStatus(true).toJSON();
} /**
* 写成txt文件格式
*
* @param path
* @throws IOException
*/
@RequestMapping(value = "/load")
public void writeTxt(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/plain");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment;filename=result.txt");
String name_ip = "这是一个txt文件";
OutputStream ops = null;
try {
ops = response.getOutputStream();
ops.write(name_ip.getBytes()); } catch (IOException e) {
// TODO Auto-generated catch block
logger.error("写结果文件异常:", e);
} finally {
ops.close();
} }

spring 上传 下載文件的更多相关文章

  1. java-spring-mvc_上傳下載文件配置及controller方法

    下载: 1.在spring-mvc中配置(用于100M以下的文件下载) <bean class="org.springframework.web.servlet.mvc.annotat ...

  2. spring mvc文件上传(单个文件上传|多个文件上传)

    单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包    1.所需jar包:                commons-fileupload-1.3.1.jar       ...

  3. Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)

            一.接收参数(postman发送) 1.form表单 @RequestParam("name") String name 会把传递过来的Form表单中的name对应 ...

  4. (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】

    文件上传主要分以下几个步骤: (1)新建maven java project: (2)在pom.xml加入相应依赖: (3)新建一个表单页面(这里使用thymleaf); (4)编写controlle ...

  5. Spring MVC-------文件上传,单文件,多文件,文件下载

    Spring MVC 框架的文件上传是基于 commons-fileupload 组件的文件上传,只不过 Spring MVC 框架在原有文件上传组件上做了进一步封装,简化了文件上传的代码实现,取消了 ...

  6. 11、只允许在主目录下上传和下载文件,不允许用putty登录

    创建用户xiao,   使其只允许在用户主目录 (/var/www/html)下上传和下载文件,不允许用putty登录 (为了安全起见,不给过多的权限) 1.创建xiao用户 [root@localh ...

  7. Xshell5下利用sftp上传下载传输文件

    sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH ...

  8. spring上传文件

    在使用spring上传文件的时候,使用的文件接收参数类型为 org.springframework.web.multipart.MultipartFile 如果该参数没有指定@RequestParam ...

  9. linux下实现ftp匿名用户的上传和下载文件功能

    1.配置/etc//vsftpd/vsftpd.conf 文件如下: 打开文件,改变如下选项,如果文件中没有该选项,需要自己手动编写该选项 write_enable=YES anonymous_ena ...

随机推荐

  1. [LeetCode] Find All Numbers Disappeared in an Array 找出数组中所有消失的数字

    Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and ot ...

  2. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  3. [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  4. ASP.NET Core MVC 在linux上的创建及发布

    前言 ASP.NET core转眼都发布半月多了,社区最近也是非常活跃,虽然最近从事python工作,但也一直对.NET念念不忘,看过了园区大神们搭建的Asp.net core项目之后,自己也是跃跃欲 ...

  5. 调用altera IP核的仿真流程—下

    调用altera IP核的仿真流程—下 编译 在 WorkSpace 窗口的 counter_tst.v上点击右键,如果选择Compile selected 则编译选中的文件,Compile All是 ...

  6. CentOS下配置java环境变量classpath

    一. 需要配置的环境变量1. PATH环境变量.作用是指定命令搜索路径,在shell下面执行命令时,它会到PATH变量所指定的路径中查找看是否能找到相应的命令程序.我们需要把 jdk安装目录下的bin ...

  7. Android技术分享收集

    Android高工必备技能! 我的 Android 开发实战经验总结 微信Android客户端架构演进之路 微信Android版智能心跳方案 流量优化: WebP 探寻之路 HTTP 协议缓存机制详解 ...

  8. MySQL基础

    数据库操作 ---终端使用数据库 mysql -u root -p 之后回车键 输入密码 ---显示所有数据库: show databases; ---默认数据库: mysql - 用户权限相关数据 ...

  9. Scala元组

    object TupleTest { def basic(firstName: String, lastName: String, age: Int): (String, String, Int) = ...

  10. 初次认识 C# win32 api

    第一次接触win32api,刚开始的时候有点迷迷糊糊的. Windows API 就是windows应用程序接口. win api向上就是windows应用程序,向下就是windows操作系统核心. ...