leadu_Java Web项目中文件上传、下载功能
2016-11-15 11:49
495 查看
package com.tm.youlingbao.controller; import com.tm.youlingbao.config.FileUploadProperties; import com.tm.youlingbao.dto.message.Message; import com.tm.youlingbao.service.FileUploadService; import org.apache.commons.io.IOUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @RestController @RequestMapping("/file") public class FileController { @Autowired private FileUploadService fileUploadService; @Autowired private FileUploadProperties fileUploadProperties; /** * 文件上传 * @param type * @param file * @return */ @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) public ResponseEntity<Message> uploadFile(String type, MultipartFile file){ return fileUploadService.uploadFile(type, file); } /** * 获取图片 * @param fileName * @return */ @RequestMapping(value = "/getImg", method = RequestMethod.GET) public byte[] getImg(String fileName){ String path = fileUploadProperties.getImgAbsolutePath() + fileName; Resource imgRes = new FileSystemResource(path); try { return IOUtils.toByteArray(imgRes.getInputStream()); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 获取录音 * @param fileName * @return */ @RequestMapping(value = "/getVoice", method = RequestMethod.GET) public byte[] getVoice(String fileName){ String path = fileUploadProperties.getImgAbsolutePath() + fileName; Resource imgRes = new FileSystemResource(path); try { return IOUtils.toByteArray(imgRes.getInputStream()); } catch (IOException e) { e.printStackTrace(); } return null; } }
package com.tm.youlingbao.service; import com.google.common.collect.Maps; import com.tm.youlingbao.config.FileUploadProperties; import com.tm.youlingbao.dto.message.Message; import com.tm.youlingbao.dto.message.MessageType; import com.tm.youlingbao.utils.CommonUtils; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.util.Map; import java.util.UUID; @Service public class FileUploadService { @Autowired private FileUploadProperties fileUploadProperties; /** * 文件上传转发器 * @param type * @param file * @return */ public ResponseEntity<Message> uploadFile(String type, MultipartFile file){ if(type.equals("img")){ return saveFile(fileUploadProperties.getImgRelativePath(), file, fileUploadProperties.getImgAbsolutePath()); }else if(type.equals("voice")){ return saveFile(fileUploadProperties.getVoiceRelativePath(), file, fileUploadProperties.getVoiceAbsolutePath()); } return new ResponseEntity<Message>(new Message(MessageType.MSG_TYPE_ERROR, "未指定上传类型"), HttpStatus.OK); } /** * 保存文件 * @param savePath * @param file * @param serverPath * @return */ public ResponseEntity<Message> saveFile(String savePath, MultipartFile file, String serverPath){ Message message = null; if (!file.isEmpty()) { String fileName = UUID.randomUUID().toString() + CommonUtils.getFileSuffix(file.getOriginalFilename()); try { FileUtils.writeByteArrayToFile(new File(savePath + fileName), file.getBytes()); Map map = Maps.newHashMap(); map.put("url", serverPath + fileName); return new ResponseEntity<Message>(new Message(MessageType.MSG_TYPE_SUCCESS, map), HttpStatus.OK); } catch (Exception e) { return new ResponseEntity<Message>(new Message(MessageType.MSG_TYPE_ERROR, "文件上传失败"), HttpStatus.OK); } } return new ResponseEntity<Message>(new Message(MessageType.MSG_TYPE_ERROR, "文件为空,上传失败"), HttpStatus.OK); } }
相关文章推荐
- 基于Java web技术文件上传和下载功能开发实战练习视频(fileupload)
- JavaWeb实现文件上传下载功能实例解析以及项目工程示例下载
- Java Web项目中实现文件下载功能的实例教程
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(一):JspSmart之upload组件源码及使用
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- Ftp实习文件上传,下载单个件及文件夹下面所有文件,获取文件列表功能
- jsp中如何利用jspsmartupload实现文件上传下载功能
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)
- as 3.0 如何浏览文件,实现文件上传和下载功能。
- FTP文件上传下载,获取文件列表功能
- Java Web中使用JSPSmartUpload控件实现文件的上传和下载(解决了中文乱码问题)(JSP页面采用GBK编码)
- 利用Apache的commons-net实现FTP的文件读取、上传、下载、删除和移动功能
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- JAVA实现FTP服务器文件的上传,下载,删除功能
- java web项目文件下载问题
- 【贴图】功能非常完善的SANTA在线文件上传下载及批量编辑器(ASP)
- linux下实现ftp匿名用户的上传和下载文件功能
- 用Struts2更好的实现文件的上传、下载功能以及解决中文名称问题
- 利用FTPClient类实现文件的上传下载功能
- 文件的上传和下载功能