MogliFS与spring mvc结合简单示例
一、MogliFS 与Spring结合配置请参照上文
二、上传页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <form action="${pageContext.request.contextPath}/file/upload" method="post" enctype="multipart/form-data">
file1:<input type="file" name="myFile"/>
<br>
file2:<input type="file" name="myFile"/>
<br>
<input type="submit" value="上传">
</form>
三、mvc的CommonsMultipartResolver解析器配置
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value=""/>
</bean>
四、上传
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(
HttpServletRequest request, @RequestParam(value = "myFile", required = false) MultipartFile[] files) {
try {
for(int i=;i<files.length;i++){
MultipartFile file=files[i]; if(StringUtils.isNotEmpty(file.getName()) && file.getSize() > ){ ...... MojiFile mf = moji.getFile("MyFileKey"+uuid); OutputStream out = null;
InputStream in = null;
try {
out = mf.getOutputStream();
in = file.getInputStream(); byte[] bs = new byte[];
while( in.read(bs) != -){
out.write(bs);
} out.flush();
} finally {
in.close();
out.close();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}
五、下载
@RequestMapping(value = "download", method = RequestMethod.GET)
public void download(HttpServletRequest request,
HttpServletResponse response, String fileName) throws Exception {
response.setContentType("text/html;charset=UTF-8");
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
...... MojiFile mf = moji.getFile("MyFileKey" + uuid);
response.setHeader("Content-disposition", "attachment; filename="
+ new String(newFileName.getBytes("gb2312"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(mf.length()));
bis = new BufferedInputStream(mf.getInputStream());
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[];
int bytesRead;
while (- != (bytesRead = bis.read(buff, , buff.length))) {
bos.write(buff, , bytesRead);
}
bis.close();
bos.close();
}
MogliFS与spring mvc结合简单示例的更多相关文章
- 用Spring MVC开发简单的Web应用程序
1 工具与环境 借助Eclipse4.3 + Maven3.0.3构建Java Web应用程序.使用Maven内置的servlet 容器jetty,不需手工集成Web服务器到Eclipse.还帮我们自 ...
- Spring MVC之简单入门
一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...
- Spring MVC - Hello World示例
以下示例演示如何使用Spring MVC框架编写一个简单的基于Web的Hello World应用程序.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发一个 ...
- 用Spring MVC开发简单的Web应用
这个例子是来自于Gary Mak等人写的Spring攻略(第二版)第八章Spring @MVC中的一个例子,在此以学习为目的进行记录. 问题:想用Spring MVC开发一个简单的Web应用, 学习这 ...
- Spring MVC 入门教程示例 (一)
今天和大家分享下 Spring MVC 入门教程 首先还是从 HelloWorld web 工程开始 -------------------------- 1.首先创建一个Maven Web工程 ...
- 基于注解的Spring MVC的简单入门——简略版
网上关于此教程各种版本,太多太多了,因为我之前没搭过框架,最近带着两个实习生,为了帮他们搭框架,我只好...惭愧啊...基本原理的话各位自己了解下,表示我自己从来没研究过Spring的源码,所以工作了 ...
- spring mvc 注解入门示例
web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...
- spring boot thymeleaf简单示例
说实话,用起来很难受,但是人家官方推荐,咱得学 如果打成jar,这个就合适了,jsp需要容器支持 引入依赖 <dependency> <groupId>org.springfr ...
- JAVA入门[20]-Spring Data JPA简单示例
Spring 对 JPA 的支持已经非常强大,开发者只需关心核心业务逻辑的实现代码,无需过多关注 EntityManager 的创建.事务处理等 JPA 相关的处理.Spring Data JPA更是 ...
随机推荐
- Java-编程规范与代码风格
阿里巴巴 Java 开发手册 https://github.com/alibaba/p3c https://developer.aliyun.com/special/tech-java 唯品会规范 J ...
- P5658 括号树
P5658 括号树 题解 太菜了啥都不会写只能水5分数据 啥都不会写只能翻题解 题解大大我错了 我们手动找一下规律 我们设 w[ i ] 为从根节点到结点 i 对答案的贡献,也就是走到结点 i ,合 ...
- IO流概述
作用: 处理设备之间的数据传输的 I: Input(输入) , O: Output(输出) 什么是输入和输出? 我们说输入和输出问题是站在内存的角度而言 , 如果我们程序读取硬盘上的数据那么就是输入 ...
- iOS8.0如何使用Touch ID来做验证
对于Objective-C而言,只要几行代码即可搞定. 比如: #import <LocalAuthentication/LocalAuthentication.h> - (void)vi ...
- Scala 踩坑系列
scala List scala list 如果使用 list(i)的形式进行遍历,如果list数据太多,每次遍历耗时会很久. 因为有一个 head tail 的概念 . 和java的List根据角标 ...
- 编译安装python3事出错:
configure: error: no acceptable C compiler found in $PATH 问题解决 解决方法: yum intall gcc -y
- Java环境中,word文档转PDF直接打开浏览而非下载
在平台上,需要把文档直接浏览而非下载,实现方法是先把文档转为PDF文件, 但在linux系统中确实汉字字库,所以转换失败,以下是解决方法 后面正式服务器也要添加字库,不然会转换出乱码文件,处理步骤如下 ...
- Linux nohup和&后台运行,进程查看及终止,进程信息输出,控制台信息输出
nohup和&后台运行,进程查看及终止 1.nohup 用途:不挂断地运行命令. 语法:nohup Command [ Arg … ] [ & ] 无论是否将 nohup 命令的输 ...
- Paper Mark2
论文:CBAM: Convolutional Block Attention Module 论文链接 pytorch代码 论文:Approach for Fashion Style Recogniti ...
- 【Leetcode_easy】788. Rotated Digits
problem 788. Rotated Digits solution1: class Solution { public: int rotatedDigits(int N) { ; ; i< ...