spring 上传 下載文件
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 上传 下載文件的更多相关文章
- java-spring-mvc_上傳下載文件配置及controller方法
下载: 1.在spring-mvc中配置(用于100M以下的文件下载) <bean class="org.springframework.web.servlet.mvc.annotat ...
- spring mvc文件上传(单个文件上传|多个文件上传)
单个文件上传spring mvc 实现文件上传需要引入两个必须的jar包 1.所需jar包: commons-fileupload-1.3.1.jar ...
- Spring Boot之 Controller 接收参数和返回数据总结(包括上传、下载文件)
一.接收参数(postman发送) 1.form表单 @RequestParam("name") String name 会把传递过来的Form表单中的name对应 ...
- (29)Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】
文件上传主要分以下几个步骤: (1)新建maven java project: (2)在pom.xml加入相应依赖: (3)新建一个表单页面(这里使用thymleaf); (4)编写controlle ...
- Spring MVC-------文件上传,单文件,多文件,文件下载
Spring MVC 框架的文件上传是基于 commons-fileupload 组件的文件上传,只不过 Spring MVC 框架在原有文件上传组件上做了进一步封装,简化了文件上传的代码实现,取消了 ...
- 11、只允许在主目录下上传和下载文件,不允许用putty登录
创建用户xiao, 使其只允许在用户主目录 (/var/www/html)下上传和下载文件,不允许用putty登录 (为了安全起见,不给过多的权限) 1.创建xiao用户 [root@localh ...
- Xshell5下利用sftp上传下载传输文件
sftp是Secure File Transfer Protocol的缩写,安全文件传送协议.可以为传输文件提供一种安全的加密方法.sftp 与 ftp 有着几乎一样的语法和功能.SFTP 为 SSH ...
- spring上传文件
在使用spring上传文件的时候,使用的文件接收参数类型为 org.springframework.web.multipart.MultipartFile 如果该参数没有指定@RequestParam ...
- linux下实现ftp匿名用户的上传和下载文件功能
1.配置/etc//vsftpd/vsftpd.conf 文件如下: 打开文件,改变如下选项,如果文件中没有该选项,需要自己手动编写该选项 write_enable=YES anonymous_ena ...
随机推荐
- [AC自动机]【学习笔记】
Keywords Search Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)To ...
- 集合工具类:collections
collection与collections的关系? public class Collectionsextends Object collection与collections没有直接的关系,但是与集 ...
- [LeetCode] Combine Two Tables 联合两表
Table: Person +-------------+---------+ | Column Name | Type | +-------------+---------+ | PersonId ...
- [LeetCode] Candy 分糖果问题
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 开发 ASP.NET vNext 初步总结(使用Visual Studio 14 CTP1)
新特性: vNext又称MVC 6.0,不再需要依赖System.Web,占用的内存大大减少(从前无论是多么简单的一个请求,System.Web本身就要占用31KB内存). 可以self-host模式 ...
- Pairwise Sum and Divide 51nod
1305 Pairwise Sum and Divide 题目来源: HackerRank 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 有这样 ...
- 启动 Eclipse 弹出“Failed to load the JNI shared library jvm.dll”错误的解决方法
原因1:给定目录下jvm.dll不存在. 对策:(1)重新安装jre或者jdk并配置好环境变量.(2)copy一个jvm.dll放在该目录下. 原因2:eclipse的版本与jre或者jdk版本不一致 ...
- Javascript模板引擎插件收集
为什么要用JS的模板引擎,打个比方,如果你要通过接口绑定数据,最终要加进去DOM中,我们普遍的做法就是不断的+,最终append进去,但是这样的做法就是后续人员压根就没法维护.所以这时模板引擎出来了. ...
- Django知识点整理
什么是web框架 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞台来做表演. web应用 访 ...