文件上传和下载
来源:互联网 发布:工业自动化软件开发 编辑:程序博客网 时间:2024/05/09 07:33
要加commons-fileupload-1.2.2.jar、commons-io-2.4.jar。
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="104857600"/></bean>
1.文件上传
@Controller@RequestMapping(value = "/fileope")public class FileUploadController {@RequestMapping(value = "upload", method = RequestMethod.POST)public String upload(HttpServletRequest request, @RequestParam(value = "myFile", required =false) MultipartFile[] files) { try { for(int i=0;i<files.length;i++){ FileUploadUtils.upload(request, files[i]); } } catch (Exception e) { e.printStackTrace(); } return "success"; }}
public class FileUploadUtils {//默认大小 50Mpublic static final long DEFAULT_MAX_SIZE = 52428800;//默认上传的地址public static String defaultBaseDir = "upload";public static final String upload(HttpServletRequest request, MultipartFile file)throws Exception{ String filename = extractFilename(file, defaultBaseDir); File desc = getAbsoluteFile(extractUploadDir(request), filename); file.transferTo(desc); return filename;}private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException { if(uploadDir.endsWith("/")) {uploadDir = uploadDir.substring(0, uploadDir.length() - 1); } if(filename.startsWith("/")) {filename = filename.substring(0, uploadDir.length() - 1); } File desc = new File(uploadDir + "/" + filename); if(!desc.getParentFile().exists()) {desc.getParentFile().mkdirs(); } if(!desc.exists()) {desc.createNewFile(); } return desc;}public static final String extractFilename(MultipartFile file, String baseDir) throws UnsupportedEncodingException { String filename = file.getOriginalFilename(); int slashIndex = filename.indexOf("/"); if (slashIndex >= 0) {filename = filename.substring(slashIndex + 1);} filename = baseDir + "/" + filename; return filename;}public static final String extractUploadDir(HttpServletRequest request) { return request.getServletContext().getRealPath("/");}}
2.文件下载
@RequestMapping(value = "download", method = RequestMethod.GET)public static 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; String newFileName = new String(fileName.getBytes("ISO8859-1"),"UTF-8"); String ctxPath = request.getSession().getServletContext().getRealPath("/") + FileUploadUtils.defaultBaseDir; String downLoadPath = ctxPath +"/"+ newFileName; long fileLength = new File(downLoadPath).length(); response.setHeader("Content-disposition", "attachment; filename="+ new String(newFileName.getBytes("gb2312"), "ISO8859-1")); response.setHeader("Content-Length", String.valueOf(fileLength)); bis = new BufferedInputStream(new FileInputStream(downLoadPath)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[2048]; int bytesRead; while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) { bos.write(buff, 0, bytesRead); } bis.close(); bos.close();}
0 0
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- 文件上传和下载
- linux下vi操作Found a swap file by the name
- 零基础学习JAVA编程题——信号报告
- 欢迎使用CSDN-markdown编辑器
- java集合之集合简介
- 高仿小米时钟
- 文件上传和下载
- mysql 主从备份 主机重启 1236的错
- smarty静态缓存
- CUDA 问题解决 ——在 c 文件中调用 cu 文件中定义的函数失败
- GBDT回归的python官方例子详解
- 公有云和私有云的区别
- 亮风台HiAR Glasses荣获“设计界奥斯卡”红点奖
- 设计模式-桥梁模式
- Java中Runnable和Thread的区别