添加 commons-fileupload-1.2.2.jar 和 commons-io-2.4.jar架包
=============applicationContext.xml=================================
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
<!-- <property name="maxUploadSize" value="5242440"></property> -->
</bean>
=============jsp===================================
<style type="text/css">
.yourName{
background-color: #9AFF9A;
border-radius: 20%/80%;
outline: none;
border:none;
font-size: 22px;
padding: 5px 10px 5px 10px;
vertical-align: top;}
.yourClassName{
text-align: left;
border: none;
outline: none;
overflow:hidden; /* 隐藏滚动条 */
resize: none;
width: 80%;
font-size: 22px;
}
</style>
//上传
<form action="major/upload" method="post" enctype="multipart/form-data" >
<button type="button" onClick="txtfilePath.click()" class="yourName">上传</button>
<textarea rows="3" class="yourClassName" name="filePath" id="filePath" readonly></textarea>
<input type="file" id="txtfilePath" name="fileName" style="display:none;"><br/>
<button type="submit" class="yourName" style="outline: none;background-color: #9f9;">确认</button></form>
//下载
<form action="major/down?fileName=${list.fileName }" method="post">
<button type="submit" class="yourName">下载</button></form>
<script type="text/javascript">
document.getElementById("txtfilePath").onchange = function() {
//将文件名赋给id=filePath的标签
document.getElementById("filePath").value = this.value.substring(this.value.lastIndexOf("\\")+1);
};</script>
=======Controller=======================
//上传
@RequestMapping("upload")
public String upload(HttpServletRequest request,String patientName,String declarationDepartment,String declarationDate,String surgeryName,String surgeryDate,MultipartFile fileName) throws ParseException {
//文件原名
String fname = fileName.getOriginalFilename();
//去掉文件名的扩展名
String prefix=fname.substring(0,fname.lastIndexOf("."));
//获取文件名的扩展名
String prefixs=fname.substring(fname.lastIndexOf("."));
//文件新名
String name=prefix+"_"+UUID.randomUUID()+prefixs;
//保存路径 :桌面/医务申请表/....String path = FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath()+"/医务申请表/重
大(特 殊)手术审批单/";
File f = new File(path);
if (!f.exists()) {
f.mkdirs();
}
if (!fileName.isEmpty()) {
try {
//文件上传
FileOutputStream fos = new FileOutputStream(path + name);
InputStream in = fileName.getInputStream();
int b = 0;
while ((b = in.read()) != -1) {
fos.write(b);
}
fos.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return "redirect:list";}
//下载
@RequestMapping(value="/down",method=RequestMethod.POST)
public void downFile(HttpServletRequest request,HttpServletResponse response,String fileName) throws IOException {
// 获取上传文件的目录
String path = FileSystemView.getFileSystemView().getHomeDirectory().getAbsolutePath()+"/医务申请表/重大(特殊)手术审批单/";
System.out.println(path + fileName);
try {
//fileName = new String(fileName.getBytes("iso8859-1"), "UTF-8");
// 得到要下载的文件
File file = new File(path + fileName);
// 如果文件不存在
if (!file.exists()) {
response.setContentType("text/html; charset=UTF-8"); //转码
PrintWriter out = response.getWriter();
out.println("<script>");
out.println("alert('您要下载的资源已被删除!!');");
out.println("history.back();");
out.println("</script>");
out.flush();
out.close();
}
else {
// 处理文件名
//String realname = fileName.substring(fileName.indexOf("_") - 1);
// 设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename="
+ URLEncoder.encode(fileName, "UTF-8"));
// 读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(path + fileName);
// 创建输出流
OutputStream out = response.getOutputStream();
// 创建缓冲区
byte buffer[] = new byte[1024];
int len = 0;
// 循环将输入流中的内容读取到缓冲区当中
while ((len = in.read(buffer)) > 0) {
// 输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
// 关闭文件输入流
in.close();
// 关闭输出流
out.close();
}
} catch (Exception e) {
}
}