最简单的SSM单个文件的上传下载操作
来源:互联网 发布:啥录屏软件好 编辑:程序博客网 时间:2024/05/21 18:47
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.ClassInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.qf.domain.CrmClass;
import com.qf.service.ClassInfoServcie;
@Controller
public class uploadController {
@Autowired
private ClassInfoServcie infoServcie;
@RequestMapping(value = "/upload/upload")
public void uploadPic(String classid,@RequestParam("uploadpath") MultipartFile file,HttpServletResponse response,HttpServletRequest request) throws IOException{
//处理上传的文件
//1. 获取文件存储的路径 - 项目根目录的upload子目录
String dirPath = request.getServletContext().getRealPath("/upload");
File dir = new File(dirPath);
if(!dir.exists()) {
dir.mkdir(); //如果目录不存在,则创建目录
}
//扩展名
String fileName=file.getOriginalFilename();
System.out.println(fileName);
//String ext = FilenameUtils.getExtension(file.getOriginalFilename());
String ext=fileName.substring(fileName.lastIndexOf("."));
//图片名称生成策略
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
//图片名称一部分
String format = df.format(new Date());
//随机三位数
Random r = new Random();
// n 1000 0-999 99
for(int i=0 ; i<3 ;i++){
format += r.nextInt(10);
}
fileName=format+"."+ext;
try {
file.transferTo(new File(dir,fileName));
System.out.println("--文件上传成功!--");
} catch (IOException e) {
e.printStackTrace();
}
//返回二个路径
CrmClass info=new CrmClass();
info.setUploadtime(new Date());
info.setUploadfilename(fileName);
info.setUploadpath("/upload/"+fileName);
info.setClassid(classid);
int rows=infoServcie.updateUploadInfo(info);
if(rows>0){
response.getWriter().print("success");
}
}
@RequestMapping("/downFile")
public void downFile(HttpServletRequest request,
HttpServletResponse response) {
// 得到要下载的文件名
String fileName = request.getParameter("filename");
try {
fileName = new String(fileName.getBytes("iso8859-1"), "UTF-8");
// 获取上传文件的目录
ServletContext sc = request.getSession().getServletContext();
// 上传位置
String rootPath = sc.getRealPath("/upload");
System.out.println(rootPath + "\\" + fileName);
// 得到要下载的文件
File file = new File(rootPath + "\\" + fileName);
// 如果文件不存在
if (!file.exists()) {
request.setAttribute("message", "您要下载的资源已被删除!!");
System.out.println("您要下载的资源已被删除!!");
return;
}
// 处理文件名
String realname = fileName.substring(fileName.indexOf("_") + 1);
// 设置响应头,控制浏览器下载该文件
response.setHeader("content-disposition", "attachment;filename="
+ URLEncoder.encode(realname, "UTF-8"));
// 读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(rootPath + "\\" + 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) {
e.printStackTrace();
}
}
}
upload.jsp----------------------------------------------------------
<c:if test="${not empty info.uploadpath }"><form action="${pageContext.request.contextPath}/downFile.do" id="download" method="get"><input type="hidden" value="${info.uploadfilename }" name="filename"><a href="${pageContext.request.contextPath}/classinfo/uploadUI.do?id=${info.classid}&name=${info.courseType.coursename }&typeid=${info.courseType.coursetypeid }">上传</a> <a href="javascript:void(0)" onclick="document.getElementById('download').submit();">下载</a></form></c:if><c:if test="${empty info.uploadpath }"><a href="${pageContext.request.contextPath}/classinfo/uploadUI.do?id=${info.classid}&name=${info.courseType.coursename }&typeid=${info.courseType.coursetypeid }">上传</a> 暂无</c:if></td>这样写是为了让这几种操作在一行显示。。
- 最简单的SSM单个文件的上传下载操作
- 简单单个文件上传下载
- 适用于ssm框架的文件上传下载
- SSM/angularjs _ 文件的上传下载
- 最详细的文件上传下载
- 最简单的文件操作
- SEO单个网页的最优化操作
- ssm 中文件的上传下载
- javaWeb最全的文件上传下载
- C语言最简单的文件操作
- SSM实现最简单的登录验证
- 用C#压缩单个txt文件的最简单方式是什么
- 单个文件上传的案例操作
- SSM第一篇 最简单的SSM框架搭建过程--SSM简单整合
- asp.net mvc3 简单的文件上传下载
- JavaWeb实现简单的文件上传下载
- struts2开发文件上传下载的简单实例
- 文件上传下载——通过struts的FormFile上传单个excel文件
- hdu4287 Intelligent IME(字典树)
- 服务器和Servlet快速入门
- SDNU 1017 (冒泡排序法)
- linux系统基本命令之文件归档,压缩以及传输
- OPNET 中断类型和中断码的理解
- 最简单的SSM单个文件的上传下载操作
- hack技术
- 深度学习框架TensorFlow学习笔记(1)
- Linux文本操作三剑客(sed, awk, grep)——awk
- windows下安装配置TensorFlow,caffe的Python接口
- Java:比较两个数的大小(条件运算符)
- 记一个关于C语言函数指针使用的小例子
- Airsim第一次build遇到的问题总结
- Mysql 增加,删除外键。