java文件上传下载
来源:互联网 发布:网络大v 编辑:程序博客网 时间:2024/06/05 15:29
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.multipart.MultipartFile;/** * 上传文档 * @param upExcel * @return */ @RequestMapping(value="/formExcel",method = RequestMethod.POST) @ResponseBody public void formExcel(MultipartFile upExcel,String fileName ){ System.out.println(upExcel.getOriginalFilename()+"----"+fileName); try { String originalFilename = fileName; if(originalFilename==null||originalFilename==""){// return "请选择要上传的文件";// this.renderJson(json(null,1,"请选择要上传的文件")); this.printFailure(1, "请选择要上传的文件"); return; } if(originalFilename.endsWith(".doc")||originalFilename.endsWith(".docx")|| originalFilename.endsWith(".pdf")||originalFilename.endsWith(".xlsx")||originalFilename.endsWith(".xls")|| originalFilename.endsWith(".ppt")||originalFilename.endsWith(".pptx")){ String uploadPath = "upload/document"+ "/" + UUID.getUUID() + originalFilename.substring(originalFilename.lastIndexOf(".")); // String uploadPath = "upload/document"+ "/" + UUID.getUUID()+originalFilename; FileStorageHelper.transferFile(upExcel.getInputStream(), uploadPath); Document document = new Document(); String[] split = originalFilename.split("\\."); String string = split[0]; document.setDesn(string); document.setUrl(uploadPath); String id = UUID.getUUID(); document.setAuthor(UserTokenHelper.getUserId()); document.setDocumentID(id); documentService.save(document);// return id;// this.renderJson(json(id,0,"上传文件成功")); this.printSuccess(id,"上传文件成功"); return; }else{// return "请选择正确格式的文档";// this.renderJson(json(null,2,"请选择正确格式的文档")); this.printFailure(2,"请选择正确格式的文档"); return; } } catch (Exception e) { e.printStackTrace();// return "上传文件失败";// this.renderJson(json(null,3,"上传文件失败")); this.printFailure(3,"上传文件失败"); return; } }
下载文件
/**import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.OutputStream;import javax.annotation.Resource;import javax.servlet.http.HttpServletResponse;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.ResponseBody; * 下载文件 * @param urldata 文件路径 * @param response */ @RequestMapping(value = "/download", method = RequestMethod.GET, produces = "application/json") @ResponseBody public void download(String urldata,String name,HttpServletResponse response){// String rootPath = SpringMVCUtil.getRequest().getSession().getServletContext().getRealPath("/");// 获取项目根目录 try { File file = new File(urldata);// path是根据日志路径和文件名拼接出来的 String filename = file.getName();// 获取文件名称 String[] split = filename.split("\\."); String string = split[1]; String nameNew = name+"."+string; InputStream fis = new BufferedInputStream(new FileInputStream(urldata)); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); response.reset(); // 先去掉文件名称中的空格,然后转换编码格式为utf-8,保证不出现乱码,这个文件名称用于浏览器的下载框中自动显示的文件名 //response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1")); response.addHeader("Content-Disposition", "attachment;filename=" + new String(nameNew.replaceAll(" ", "").getBytes("utf-8"),"iso8859-1")); response.addHeader("Content-Length", "" + file.length()); OutputStream os = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); os.write(buffer);// 输出文件 os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } }
阅读全文
1 0
- java文件上传下载
- java文件上传下载
- Java文件上传下载 源码
- java文件上传下载
- Java文件上传下载 源码
- 文件上传下载代码 java
- java文件上传下载
- java-web文件上传下载
- java上传下载文件
- java文件上传下载
- JAVA 文件上传下载
- Java 文件的上传下载
- java实现文件上传下载
- java文件上传下载
- java ftp 文件上传下载
- java socket 文件上传下载
- java文件上传下载
- java FTP上传下载文件
- 深入理解机器学习中拉格朗日乘子和KKT条件
- 解决mysql 5.5以上乱码问题
- HTML随堂笔记
- php聊天室简单实现
- java调用copy复制子文件夹及文件到指定目录(非xcopy)
- java文件上传下载
- ContentProvider的详解
- c/c++下 long long与__int64不同编译环境的比较
- LimeSDR-USB Quick Test
- hello,我的程序员博客起航了
- 洛谷 1078 文化之旅
- php 身份证判断是否成年
- Chrome 控制台console的用法
- # Android Studio 混淆报错 libhyphenate_av.so