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] Boom Enemy 炸弹人

    Given a 2D grid, each cell is either a wall 'W', an enemy 'E' or empty '0' (the number zero), return ...

  2. [LeetCode] Nim Game 尼姆游戏

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  3. DDNS动态更新

    /// <summary> /// DnsPod DDNS工具类 /// </summary> public class DDNSToolkit { private stati ...

  4. PHP执行文档操作

    1.POWINTPOINT系列 之前参与过一个商城的项目,里面有将excel 导出的功能,但是如果要弄成PPT的我们应该怎么办呢?PHP是属于服务器端的 总不能在里面装个Powintpoint吧.于是 ...

  5. How to Disable Strict SQL Mode in MySQL 5.7

    If your app was written for older versions of MySQL and is not compatible with strict SQL mode in My ...

  6. hTML5实现表单内的上传文件框,上传前预览图片,针刷新预览images

    hTML5实现表单内的上传文件框,上传前预览图片,针刷新预览images, 本例子主要是使用HTML5 的File API,建立一個可存取到该file的url, 一个空的img标签,ID为img0,把 ...

  7. 【Codeforces235C】Cyclical Quest 后缀自动机

    C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...

  8. js控制Bootstrap 模态框(Modal)插件

    js控制Bootstrap 模态框(Modal)插件 http://www.cnblogs.com/zzjeny/p/5564400.html

  9. HTML 全局属性

    http://www.w3school.com.cn/tags/html_ref_standardattributes.asp

  10. ApacheCommons的Java公共类库(实现如Log这些功能)

    Apache Commons是Apache软件基金会的项目,曾隶属于Jakarta项目.Commons的目的是提供可重用的.开源的Java代码. 解释:http://baike.baidu.com/i ...