文件上传
- 利用org.springframework.web.multipart.MultipartFile类(该方法存在一定不足)
/**
* 该方法属于上传保存后,得到保存后的路径
* @param file
* @return
*/
public String videoUpload(MultipartFile file) {
String realPath = MyConstants.COMMON_EMPTY;
String saveUrl = MyConstants.COMMON_EMPTY;
try {
String path = request.getSession().getServletContext().getRealPath("/") + MyConstants.UPLOAD_VIDEO;
File dir = new File(path);
if (!dir.exists()) {
dir.mkdir();
}
String date = String.valueOf(System.currentTimeMillis());
String filename = file.getOriginalFilename();
String prefix=filename.substring(filename.lastIndexOf(".")+1);
String data = UUID.randomUUID().toString();
realPath = path + date + data + MyConstants.VIDEO_DIRECTORY + "." + prefix;
File f = new File(realPath);
file.transferTo(f);
saveUrl = MyConstants.UPLOAD_VIDEO+ date + data + MyConstants.VIDEO_DIRECTORY + "." + prefix;
} catch (Exception e) {
e.printStackTrace();
}
return saveUrl;
}
- 利用org.springframework.web.multipart.MultipartHttpServletRequest类
/**
* 视频上传
* @param file
* @return
*/
public String videoUpload(MultipartHttpServletRequest multiReq) {
String realPath = MyConstants.COMMON_EMPTY;
String saveUrl = MyConstants.COMMON_EMPTY;
String filename = multiReq.getFile("file").getOriginalFilename();
String prefix=filename.substring(filename.lastIndexOf(".")+1);
FileOutputStream fos = null;
FileInputStream fis = null;
try {
String path = request.getSession().getServletContext().getRealPath("/") + MyConstants.UPLOAD_VIDEO;
fis = (FileInputStream) multiReq.getFile("file").getInputStream();
String date = String.valueOf(System.currentTimeMillis());
String data = UUID.randomUUID().toString();
realPath = path + date + data + MyConstants.VIDEO_DIRECTORY + "." + prefix;
fos = new FileOutputStream(realPath);
byte[] temp = new byte[1024];
int i = fis.read(temp);
while (i != -1){
fos.write(temp,0,temp.length);
fos.flush();
i = fis.read(temp);
}
saveUrl = MyConstants.UPLOAD_VIDEO+ date + data + MyConstants.VIDEO_DIRECTORY + "." + prefix;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return saveUrl;
}
然后利用返回的路径,拼接相对路径和绝对路径,为文件的显示和删除做准备
/**
* 上传视频
* @param file
*/
@RequestMapping(value = "/uploadcourseVideo", method = RequestMethod.POST)
public @ResponseBody void uploadVideo(MultipartHttpServletRequest multiReq) {
String port = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
String realpath = this.videoUpload(multiReq);
String path = request.getSession().getServletContext().getRealPath("/");
String relativePath = port + realpath;
String absolutePath = path + realpath;
FileUploadRotation fileUploadRotation = new FileUploadRotation();
if("".equals(realpath)) {
fileUploadRotation.setCode("0");
fileUploadRotation.setMsg("视频上传失败");
} else {
fileUploadRotation.setAbsolutePath(absolutePath);
fileUploadRotation.setRelativePath(relativePath);
fileUploadRotation.setCode("1");
}
outPrintFileUpload(fileUploadRotation);
}