采用流的形式存储图片进行上传图片

33 篇文章 0 订阅
29 篇文章 0 订阅


// 上传图片
@RequestMapping("/upload")

public void upload(@RequestParam(value="file",required=false)MultipartFile file,HttpServletRequest request,ModelMap model, HttpServletResponse response,String id) throws IOException, SQLException{
Criteria ca = new Criteria("id", id);
log.info("开始上传机构图片");
Organization org = organizationService.getOrganization(ca);
org.setUpdatetime(new Date());
String newFileName =null;
String exe =null;

String path = request.getSession().getServletContext().getRealPath("/resources/images/upload");
Map<String, Object> map = new HashMap<String, Object>();
if(file.getOriginalFilename().length()==0){
MessageUtils.outputJSONResult("fail",response);
return;
}
else if(file!=null){
String fileName=file.getOriginalFilename();
exe = FilenameUtils.getExtension(fileName);
newFileName = UUID.randomUUID().toString();

File targetFile=new File(path+"\\"+newFileName+"."+exe);


if(!targetFile.exists()){
targetFile.mkdirs();
}
try {
file.transferTo(targetFile);
} catch (Exception  e) {

e.printStackTrace();
log.error("上传失败");
}
log.info("上传路径和文件名"+request.getContextPath()+"/images/upload/"+fileName);
model.addAttribute("fileUrl", request.getContextPath()+"/images/upload/"+fileName+".jsp");
//request.getSession().setAttribute("fileName",request.getContextPath()+"/images/upload/"+newFileName+"."+exe);
map.put("fileUrl", request.getContextPath()+"/images/upload/"+newFileName+"."+exe);

}
String url = ConfigurationHelper.getDbUrl();
String userName = ConfigurationHelper.getUserName();
String password = ConfigurationHelper.getPassword();
Connection conn=null;
PreparedStatement statement = null;
try {
conn = DriverManager.getConnection(url, userName, password);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
log.error("数据库连接失败");
}
if(request.getParameter("s")!=null){
String sql="update organization set THUMBNAIL=? where id="+id;
statement = conn.prepareStatement(sql);
statement.setBlob(1, new FileInputStream(path+"\\"+newFileName+"."+exe));
model.addAttribute("log", "thumbnail");
}else{
String sql="update organization set PICTURE=? where id="+id;
statement = conn.prepareStatement(sql);
statement.setBlob(1, new FileInputStream(path+"\\"+newFileName+"."+exe));
model.addAttribute("log", "pic");
}

statement.executeUpdate();
statement.close();
//deleteFile(path);
MessageUtils.outputJSONResult("success",response);

//return "organization.upload";
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值