1.vm
<html>
<title></title>
<head >
</head>
<body >
<form action="uploadFile" method="Post" enctype="multipart/form-data">
<input name="file" type="file" id="file"/>
<br><br>
<input type="submit" value="开始长传"/>
<form>
</body>
</html>
2.下载页面
<html>
<title></title>
<head >
</head>
<body >
$!{uploadFileName}
$!{length}
$!{file}
<hr>
<a href="down?path=$!{path}/$!{uploadFileName}">下载</a>
</body>
</html>
3.Controller
package com.shangchuan.controllers;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import org.springframework.web.multipart.MultipartFile;
@Path("upload")
public class UploadController {
public String shows() {
return "upload";
}
@Post("uploadFile")
public String uploadFile(Invocation inv, HttpServletRequest request, HttpServletResponse response, @Param("file") MultipartFile file) {
String path = request.getSession().getServletContext().getRealPath("/upload");
try {
InputStream in;
in = file.getInputStream();
FileOutputStream fos = new FileOutputStream(new File(path, file.getOriginalFilename()));
byte b[] = new byte[(int) file.getBytes().length];
file.getInputStream().read(b);
fos.write(b);
fos.close();
inv.addModel("uploadFileName", file.getOriginalFilename());// 上次信息文件名
inv.addModel("length", file.getBytes().length);// 上次文件大小
inv.addModel("path", path);
inv.addModel("file", file);
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return "success";
}
@Get("down")
public String down(Invocation inv, HttpServletResponse response, HttpServletRequest requ, @Param("path") String path) throws IOException {
try {
File file = new File(path);
// 取得文件名。
String filename = file.getName();
System.out.println(filename + " :filename");
// 取得文件的后缀名。
String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(path));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
// 设置response的Header
// inline 在线打开
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
// response.addHeader("Content-Disposition",
// "attachment;filename=filename.ext");
response.addHeader("Content-Length", "" + file.length());
// 当代码里面使用Content-Disposition来确保浏览器弹出下载对话框的时候。
// response.addHeader("Content-Disposition","attachment");一定要确保没有做过关于禁止浏览器缓存的操作。如下:
// response.setHeader("Pragma", "No-cache");
// response.setHeader("Cache-Control", "No-cache");
// response.setDateHeader("Expires", 0);
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");// 提示你是打开还是保存
toClient.write(buffer);
toClient.flush();
toClient.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
return "";
}
}