上传文件简单示例
来源:互联网 发布:支票管理打印软件 编辑:程序博客网 时间:2024/05/17 21:52
@ResponseBody
@RequestMapping("/uploadNoticeImg")
public String uploadNoticeImage(@RequestParam MultipartFile file,HttpServletRequest request) {
//要上传的地址
//String path = AppPropertiesUtil.getValue("imgUrl");
String path = request.getSession().getServletContext().getRealPath("/")+"upload/";
path += DateUtil.getDate(new Date()) + "/";
//上传图片
File newfile=new File(path);
newfile.mkdirs();
//上传后图片名称
String name=UUID.randomUUID().toString();
//上传前图片名称
String oldName=file.getOriginalFilename();
//后缀名
String extName=oldName.substring(oldName.lastIndexOf(".")+1, oldName.length());
File fileResoult= new File(path+"/"+name+"."+extName);
FileOutputStream fop1 = null;
try {
fop1 = new FileOutputStream(fileResoult);
byte[] b=file.getBytes();
fop1.write(b);
fop1.flush();
fop1.close();
logger.info("====================upload picture success==========================");
} catch (Exception e) {
logger.error("====================upload picture error==========================");
e.printStackTrace();
}finally {
if(null!=fop1){
try {
fop1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//图片查看地址
String viewUrl=AppPropertiesUtil.getValue("domainImgUrl");
viewUrl += DateUtil.getDate(new Date()) + "/";
viewUrl=viewUrl+name+"."+extName;
logger.info("====================upload picture url is "+viewUrl);
return viewUrl;
}
@RequestMapping("/uploadNoticeImg")
public String uploadNoticeImage(@RequestParam MultipartFile file,HttpServletRequest request) {
//要上传的地址
//String path = AppPropertiesUtil.getValue("imgUrl");
String path = request.getSession().getServletContext().getRealPath("/")+"upload/";
path += DateUtil.getDate(new Date()) + "/";
//上传图片
File newfile=new File(path);
newfile.mkdirs();
//上传后图片名称
String name=UUID.randomUUID().toString();
//上传前图片名称
String oldName=file.getOriginalFilename();
//后缀名
String extName=oldName.substring(oldName.lastIndexOf(".")+1, oldName.length());
File fileResoult= new File(path+"/"+name+"."+extName);
FileOutputStream fop1 = null;
try {
fop1 = new FileOutputStream(fileResoult);
byte[] b=file.getBytes();
fop1.write(b);
fop1.flush();
fop1.close();
logger.info("====================upload picture success==========================");
} catch (Exception e) {
logger.error("====================upload picture error==========================");
e.printStackTrace();
}finally {
if(null!=fop1){
try {
fop1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//图片查看地址
String viewUrl=AppPropertiesUtil.getValue("domainImgUrl");
viewUrl += DateUtil.getDate(new Date()) + "/";
viewUrl=viewUrl+name+"."+extName;
logger.info("====================upload picture url is "+viewUrl);
return viewUrl;
}
阅读全文
0 0
- 上传文件简单示例
- fileupload上传文件简单示例
- php文件上传简单示例
- 简单的文件上传示例
- struts2文件上传简单示例
- 最简单的html文件上传示例
- Struts2文件上传的简单示例
- spring MVC 文件上传简单示例
- php文件上传最简单的上传代码示例
- servlet简单上传示例
- 一个简单的PHP文件上传示例程序
- 示例:文件上传
- Struts2上传文件示例
- FTP上传文件示例
- FTP上传文件示例
- 文件上传示例
- struts2上传文件示例
- 文件上传的示例
- ES6 Proxy杂记
- 多隆:从工程师到合伙人 | 阿里技术人纪录片
- Django开发速成
- Android更换logo之后小米手机和部分三星手机通知栏的小图标未显示正确的图标问题
- 人脸识别之人脸对齐(六)--ERT算法
- 上传文件简单示例
- C语言中各种运算符的优先级
- mysql之为已经建好的表格添加id为自增主键
- 面试时,发现公司有这8个现象,建议你慎重考虑
- ios自动统计代码行数
- 页面指定区域懒加载
- ubuntu16.04 NFS系统挂载
- 以太坊客户端Geth命令用法-参数详解
- TouchEvent