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. Life

    24小时热水.24小时无线WiFi.24小时空调:有人陪你,不孤单,不寂寞,不瞎想,不停的传播正能量

  2. mysql max_allowed_packet 设置过小导致记录写入失败

    mysql根据配置文件会限制server接受的数据包大小. 有时候大的插入和更新会受max_allowed_packet 参数限制,导致写入或者更新失败. 查看目前配置 show VARIABLES ...

  3. 台式机装原版Win2008R2

    台式机装原版Win2008R2 坑了老半天,总结出几点 1,系统os下载: http://msdn.itellyou.cn/ 注:其他地方下载的,装后发现不是起不来就是驱动装不了. 2,u盘里放个压缩 ...

  4. [原]CentOS7部署PostGis

    转载请注明原作者(think8848)和出处(http://think8848.cnblogs.com) 本文参考了<An almost idiot's guide to install Pos ...

  5. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  6. [LeetCode] Combination Sum III 组合之和之三

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  7. 讲座:Influence maximization on big social graph

    Influence maximization on big social graph Fanju PPT链接: social influence booming of online social ne ...

  8. Jquery揭秘系列:实现$.fn.extend 和$.extend函数

    前面我们扩展了bind方法和ready函数,这次我要讲一下$.fn.extend 和$.extend函数. 其他的不多说,直接切入主题吧! 先来看看这两个函数的区别: $.fn.extend是为查询的 ...

  9. css3-无缝滚动

    @keyframes 规则用于创建动画.在 @keyframes 中规定某项 CSS 样式,就能创建由当前样式逐渐改为新样式的动画效果. 动画的名称和运行所需时间是必须的 帧动画:将动画名称赋给选择器 ...

  10. iOS 10.0适配之旅

    1.升级Xcode体验 升级到Xcode之后,调试程序好多东西都不是太适应 控制台莫名给你打印一堆不是太好理解的东西 之前使用 Alcatraz 下载的插件都不能用(如何使用Alcatraz) 打开麦 ...