springboot图片上传

阅读: 评论:0

springboot图片上传

springboot图片上传

js中图片上传回显功能

方法一:(mike)
$filePath&#ateObjectURL(this.files[0]);
$('#editDevicePhoto').attr('src',$filePath);
方法二:
//获取点击的文本框
var file &#ElementById("file");
var imgUrl =ateObjectURL(file.files[0]);
var img &#ElementById('imghead');
img.setAttribute('src',imgUrl); // 修改img标签src属性值

ajax处理

方法一:(mike)
var formData = new FormData();
formData.append("id",$("#selectDeviceId").val())
formData.append("file",file);//2 上传设备数据$.ajax({type: 'POST',url: '/decive/uploadDeviceImage',data: formData,dataType: 'JSON',cache: false,processData: false,contentType: false,success: function(data) {.....}
方法二:
var formData = new FormData($( "#uploadForm" )[0]);  $.ajax({  url: '<%=basePath%>UploadImage' ,  /*这是处理文件上传的servlet*/type: 'POST',  data: formData,  async: false,  cache: false,  contentType: false,  processData: false,  success: function (returndata) {//这是预览图片用的ElementById("showpic").src="<%=basePath%>UploadImage?picture=showpic";}

后台处理:

方法一:(mike)
	@ResponseBody@RequestMapping(value = "/uploadDeviceImage")public ReturnData uploadImage(MultipartFile file, String id) {ReturnData rd = new ReturnData();try {if (file != null) {jsupload(file, id);rd = new ReturnData().success();}} catch (Exception e) {rd = new ReturnData().fail();}return rd;}public void jsupload(MultipartFile multipartFile, String id) throws IOException {if (!multipartFile.isEmpty()) {String saveName = "";String saveSrc = "";//saveSrc = getKey_Src();saveSrc = getPicture_Src();saveName = id + ".png";byte[] bytes = Bytes();BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(saveSrc + saveName)));bufferedOutputStream.write(bytes);bufferedOutputStream.close();}}public String getPicture_Src(){String server_path = Properties("web.upload-img-path");String server_site_path = Properties("web.upload-picture-path");File f = new File(server_path + server_site_path);if (!f.exists())f.mkdirs();return server_path + server_site_path;}
方法二:
    @RequestMapping("/uploadPic")@ResponseBodypublic ResponseView  addProduct(MultipartFile file, Integer picNum){ResponseView rv = new ResponseView();//构建文件目录File uploadDir = new File("F:/DreamCinemaImg");//如果目录不存在,则创建if(!ists()){uploadDir.mkdir();}//获取上传的文件名String fileName = OriginalFilename();//构建一个完整的文件上传对象File uploadFile = new AbsolutePath() + "/" + fileName);//判断文件是否存在if(!ists()) {try {//通过transferTo方法进行上传ansferTo(uploadFile);rv.setCode(0);//把文件名放入响应视图rv.setData(fileName);} catch (IOException e) {e.printStackTrace();rv.setCode(500);throw new Message());}}else{rv.setCode(400);}return rv;}

本文发布于:2024-01-30 16:53:04,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170660478521469.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23