java 通用文件上传并保存图片


/**
* 通用图片上传
*/
public void commonImgUpload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/xml; charset=utf-8");
response.setCharacterEncoding("UTF-8");

PrintWriter pw = response.getWriter();
String xml = "";

String imgName = getParam(request, "imgName");
String imgValue = getParam(request, "imgValue");
String type = getParam(request, "type");
String encryptCode = getParam(request, "encryptCode");
log.debug("图片上传,图片名称:" + imgName + ",上传类型:" + type);
if (MD5Checker.check(encryptCode, imgName)) {
try {
String path = this.getClass().getClassLoader().getResource("").getPath();
log.info("当前路径:" + path);
path = path.replace("WEB-INF/classes/", "pictures/" + type + "/");
log.info("替换后路径:" + path);
File temp = new File(path);
if (!temp.isDirectory()) {
log.info("创建目录path: " + path);
temp.mkdirs();
}
path += imgName;
log.info("图片全path: " + path);
FileUtil.saveFile(path, (new BASE64Decoder()).decodeBuffer(imgValue), "UTF-8");
xml = "success";
log.info("上传图片:" + path + "成功!");
} catch (Exception e) {
log.info("上传图片失败," + e.getMessage());
xml = "fail";
}
} else {
xml = "<data><resultCode>" + "09" + "</resultCode>" + "<resultMsg>" + "摘要验证错误" + "</resultMsg>" + "</data>";
}

pw.print(xml);
if (null != pw) {
pw.close();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值