您的位置:首页 > 编程语言 > Java开发

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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐