SpringMVC上传图片代码
来源:互联网 发布:淘宝远程安装软件 编辑:程序博客网 时间:2024/06/13 06:24
// 上传图纸
@RequestMapping(value = "/uploadPaper.do")
public @ResponseBody
Object uploadPoster(HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpJson httpJson = new HttpJson();
System.out.println("开始上传照片");
String customerId = request.getParameter("computecustomerId");
String ret_fileName = null;// 返回给前端已修改的图片名称
String base64Img = request.getParameter("base64");
// 临时文件路径
// String dirTemp = "\\upload\\temp";
String dirTemp = "\\upload";
String uploadImg = "\\upload";
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
httpJson.setMsg("上传图片失败");
httpJson.setSuccess(false);
return httpJson;
}
// response.setContentType("text/html; charset=UTF-8");
// PrintWriter out = response.getWriter();
String realPath = request.getServletContext().getRealPath("");
String tempPath = request.getServletContext().getRealPath("/")
+ dirTemp;
File file_normer = new File(realPath + uploadImg+"/"+customerId);
if (!file_normer.exists()) {
file_normer.mkdirs();
}
// 用于设置图片过大,存入临时文件
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(20 * 1024 * 1024); // 设定使用内存超过5M时,将产生临时文件并存储于临时目录中。
factory.setRepository(new File(tempPath)); // 设定存储临时文件的目录。
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
if (base64Img == null) // 图像数据为空
return false;
base64Img = base64Img.replaceAll("data:image/jpeg;base64,", "");
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(base64Img);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
ret_fileName = new String((DateHandler.dateToStr(new Date(),"yyyyMMddhhmmss")+".jpg").getBytes("gb2312"), "ISO8859-1" ) ;
File file = new File(realPath + uploadImg+"/"+ customerId + "/" + ret_fileName);
OutputStream out = new FileOutputStream(file);
out.write(b);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
String image_url = request.getSession().getServletContext() .getContextPath()+ "/upload/" + customerId + "/" + ret_fileName;
// 将已修改的图片url对应的id返回前端
httpJson.setMsg("上传图片成功");
httpJson.setSuccess(true);
httpJson.setObj(image_url);
return JSONObject.fromObject(httpJson);
}
@RequestMapping(value = "/uploadPaper.do")
public @ResponseBody
Object uploadPoster(HttpServletRequest request, HttpServletResponse response)
throws Exception {
HttpJson httpJson = new HttpJson();
System.out.println("开始上传照片");
String customerId = request.getParameter("computecustomerId");
String ret_fileName = null;// 返回给前端已修改的图片名称
String base64Img = request.getParameter("base64");
// 临时文件路径
// String dirTemp = "\\upload\\temp";
String dirTemp = "\\upload";
String uploadImg = "\\upload";
try {
request.setCharacterEncoding("UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
httpJson.setMsg("上传图片失败");
httpJson.setSuccess(false);
return httpJson;
}
// response.setContentType("text/html; charset=UTF-8");
// PrintWriter out = response.getWriter();
String realPath = request.getServletContext().getRealPath("");
String tempPath = request.getServletContext().getRealPath("/")
+ dirTemp;
File file_normer = new File(realPath + uploadImg+"/"+customerId);
if (!file_normer.exists()) {
file_normer.mkdirs();
}
// 用于设置图片过大,存入临时文件
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(20 * 1024 * 1024); // 设定使用内存超过5M时,将产生临时文件并存储于临时目录中。
factory.setRepository(new File(tempPath)); // 设定存储临时文件的目录。
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("UTF-8");
if (base64Img == null) // 图像数据为空
return false;
base64Img = base64Img.replaceAll("data:image/jpeg;base64,", "");
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] b = decoder.decodeBuffer(base64Img);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
ret_fileName = new String((DateHandler.dateToStr(new Date(),"yyyyMMddhhmmss")+".jpg").getBytes("gb2312"), "ISO8859-1" ) ;
File file = new File(realPath + uploadImg+"/"+ customerId + "/" + ret_fileName);
OutputStream out = new FileOutputStream(file);
out.write(b);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
String image_url = request.getSession().getServletContext() .getContextPath()+ "/upload/" + customerId + "/" + ret_fileName;
// 将已修改的图片url对应的id返回前端
httpJson.setMsg("上传图片成功");
httpJson.setSuccess(true);
httpJson.setObj(image_url);
return JSONObject.fromObject(httpJson);
}
0 0
- SpringMVC上传图片代码
- springmvc处理上传图片代码(校验图片尺寸、图片大小)
- springmvc处理上传图片代码(校验图片尺寸、图片大小)
- SpringMVC 上传图片
- SpringMvc上传图片
- springMVC多图片上传
- SpringMVC图片上传
- springmvc上传多图片
- springmvc多图片上传
- springmvc上传图片实例
- springmvc上传图片
- springmvc上传图片文件
- Springmvc 上传图片
- springmvc上传图片
- SpringMvc图片上传
- SpringMVC图片上传
- springmvc上传图片
- springmvc图片上传问题
- ArcEngine 连接Sql Server SDE
- TCP通信过程中各步骤的状态---(简单解释)
- .ocx安装与使用
- PyOdps 0.4版本发布,从一个故事说起
- [编程题]超级素数幂
- SpringMVC上传图片代码
- 【MOOC】数字信号处理-电子科技大学-第二周
- jxl.read.biff.biffexception: the input file was not found
- Android播放Gif图片
- 调结者(Dispatcher)之执行action
- L2-008. 最长对称子串
- 4K技术简介
- 快速指数算法
- 软键盘弹出,listview上移并滚动到最后的解决方法