[Java] 纯文本查看 复制代码/**
* 上传图片
* @param ip
* @param filename
* @return
*/
@RequestMapping(value="upload",method=RequestMethod.POST)
@ResponseBody
public String upload( @RequestParam("ip") String ip,
@RequestParam("file") MultipartFile file){
logger.info("file name " + file.getOriginalFilename());
screenShotService.upload(ip, file);
return "success";
}
/**
* 上传截屏
*
* 逻辑:
* 1,把文件转移到存储目录
* 2,生成screenshot对象保存起来
* @param ip
* @param filename
*/
public ScreenShot upload(String ip, MultipartFile file)
{
try {
String localPath = receiveFile(file);
String channel = "testChannel";
String user = "testUser";
if (StringUtils.isNotBlank(localPath)){
//创建对象
ScreenShot ss = new ScreenShot(file.getOriginalFilename(),ip,user,channel,localPath);
//保存到数据库
ss = screenShotRepository.save(ss);
return ss;
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
private String receiveFile(MultipartFile file) throws IOException {
String path;
//destPath为存储文件的目录地址,目录下文件夹和文件取名由ileUtil.buildNewFileName()完成
String destPath = FileUtil.buildNewFileName(configService.getUploadRootPath() + Const._SPLASH, file.getOriginalFilename());
logger.info("upload file Path is " + file.getOriginalFilename()
+ " dest file name is " + destPath);
//新建一个名为dest的空文件
File dest = new File(destPath);
//把file放到dest的path
file.transferTo(dest);
path = dest.getPath();
return path;
}
public ScreenShot findById(String id) {
return this.screenShotRepository.findOne(id);
}