一、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();
}