1.Controller
package com.bypx.controller;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
@RequestMapping("FileController")
public class FileController {
/***
* 实现文件的上传
*
* @RequestParam:指定客户端发送数据的名字
* **/
@RequestMapping("/uploadFile")
public String uploadFile(@RequestParam("cmf") CommonsMultipartFile cmf,
HttpSession session) {
// 1获得上传的文件内容
byte[] bytes = cmf.getBytes();
// 2获得upload的绝对路径
String path = session.getServletContext().getRealPath("/upload");
// 3在服务器的upload目录下创建File对象
String oname = cmf.getOriginalFilename(); // 上传文件的原始名字
String uuid = UUID.randomUUID().toString();
File file = new File(path, uuid
+ oname.substring(oname.lastIndexOf(".")));
// 4将上传的文件拷贝到指定的目录
try {
FileUtils.writeByteArrayToFile(file, bytes);
} catch (IOException e) {
e.printStackTrace();
}
return "index.jsp";
}
/***
* 实现文件下载
*
* @throws UnsupportedEncodingException
* ***/
@RequestMapping("/download")
public String downloadFile(@RequestParam("fileName") String fileName,
HttpServletRequest request, HttpServletResponse response) {
if (fileName != null) {
String realPath = request.getSession().getServletContext()
.getRealPath("upload");
File file = new File(realPath, fileName);
if (file.exists()) {
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader(
"Content-Disposition",
"attachment;fileName="
+ new String(fileName.getBytes("gbk"),
"iso-8859-1"));// 设置文件名
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
return null;
}
}
2.html(这个按钮,就是安一下就会下载)
{field:'fname', title:'附件',align:'center',
formatter : function(value, rows, index) {
return "<a href='/jxc/FileController/download.do?fileName="+rows.fname+"' ><b>查看</b></a>";
}
},
3.SpringMvc.xml配置文件中再加上一个就可以了
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property><!-- 客户端发送数据的编码 -->
<property name="maxUploadSize" value="5242880"></property><!-- 上传文件的大小 -->
<property name="uploadTempDir" value="/upload"></property>
</bean>