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 ...
随机推荐
- BZOJ 2588: Spoj 10628. Count on a tree [树上主席树]
2588: Spoj 10628. Count on a tree Time Limit: 12 Sec Memory Limit: 128 MBSubmit: 5217 Solved: 1233 ...
- iOS多线程主题
下面是:2个并发进程.和2个并发线程的示意图: 下面介绍三种多线程技术(Thread.Cocoa Operation.Grand Central Dispatch): 1.最轻量级Thread(需要自 ...
- ActiveMQ笔记(3):基于Networks of Brokers的HA方案
上一篇介绍了基于ZK的ActiveMQ HA方案,虽然理解起来比较容易,但是有二个不足: 1) 占用的节点数过多,1个zk集群至少3个节点,1个activemq集群也至少得3个节点,但其实正常运行时 ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- MVC系列——MVC源码学习:打造自己的MVC框架(四:了解神奇的视图引擎)
前言:通过之前的三篇介绍,我们基本上完成了从请求发出到路由匹配.再到控制器的激活,再到Action的执行这些个过程.今天还是趁热打铁,将我们的View也来完善下,也让整个系列相对完整,博主不希望烂尾. ...
- 如何创建一个简单的Visual Studio Code扩展
注:本文提到的代码示例下载地址>How to create a simple extension for VS Code VS Code 是微软推出的一款轻量级的代码编辑器,免费,开源,支持多种 ...
- C语言结构体对齐
1.结构体变量中的元素如何访问? (1)数组中元素的访问方式:表面上有2种方式(数组下标方式和指针方式):实质上都是指针方式访问.(2)结构体变量中的元素访问方式:只有一种,用.或者->的方式来 ...
- Podfile使用说明
什么是Podfile ? CocoaPods是用ruby实现的,因此Podfile文件的语法就是ruby的语法.podfile是一个说明文件,用以描述管理一个或者多个Xcode project的tar ...
- 使用winmm.dll 获取麦克风声音数据
//录音 /// <summary> /// 初始化录音环境 /// </summary> /// <returns></returns> public ...