jsp+ajax+springMvc图片上传立刻回显 电商

思路:onchange()事件触发异步请求,js将成功后返回图片的地址动态写入


JSP页面:  外层一个ID为myForm的表单

<pre name="code" class="html">  <a name="uploadImgs" id="uploadImgs"></a>
    <p><label><samp>*</samp>上传产品图片(XX尺寸):</label><span id="uploadImgTip1" class="orange">注:该尺寸图片必须为90x150。</span></p>
    <p><label></label>
		<img id='imgSize1ImgSrc' src='${path}/ecps/console/images/logo266x64.png'  height="100" width="100" />
		<input type='file' id='imgSize1File' name='imgSize1File' class="file" οnchange='submitImgSize1Upload()' /><span class="pos" id="imgSize1FileSpan">请上传图片的大小不超过3MB</span>
        <input type='hidden' id='imgSize1' name='imgSize1' value='' reg="^.+$" tip="亲!您忘记上传产品图片了。" />
	</p>


 

JS函数

function submitImgSize1Upload() {
	
	var option = {
			url:"${path}/upload/upload.do",//使用ajax的方式提交表单,url以option中为准
			type:'post',
			dataType:"text",//不要写成dateType
			data:{
				fileName:'imgSize1File'
			},
			success:function(responseText){
				//把字符串解析成json对象
				var obj = $.parseJSON(responseText);
				$("#imgSize1ImgSrc").attr("src", obj.realPath);
				$("#imgSize1").val(obj.relativePath);
				
			},
			error:function(){
				alert("系统错误");
			}
	};
	
	$("#myForm").ajaxSubmit(option);

}



控制层:

import com.sun.jersey.api.client.Client;

import com.sun.jersey.api.client.WebResource;


@RequestMapping("/upload.do")
	public void upload(String fileName, HttpServletRequest request, PrintWriter out){
		MultipartHttpServletRequest mr = (MultipartHttpServletRequest) request;
		CommonsMultipartFile cfile = (CommonsMultipartFile) mr.getFile(fileName);
		byte[] fbyte = cfile.getBytes();
		String imageName = "";
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		imageName = format.format(new Date());
		Random random = new Random();
		for(int i =0; i < 3; i++){
			imageName = imageName + random.nextInt(10);
		}
		String raginalFileName = cfile.getOriginalFilename();
		String suffixName = raginalFileName.substring(raginalFileName.lastIndexOf("."));
		//创建客户端
		Client client = Client.create();
		//图片在图片服务器上的绝对路径
		String imagePath = EcpsConstants.UPLOAD_PATH+"/upload/"+imageName+suffixName;
		WebResource resource = client.resource(imagePath);
		//把图片放到图片服务器上
		resource.put(String.class, fbyte);
		
		String result = "{\"realPath\":\""+imagePath+"\", \"relativePath\":\""+"/upload/"+imageName+suffixName+"\"}";
		out.write(result);
		out.close();
	}




  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring MVC 提供了文件上传的支持,可以使用 MultipartFile 类型来接收上传的文件。以下是图片上传回显的步骤: 1. 在前端页面中添加一个表单,其中包含一个文件选择器用于选择要上传的图片文件,并设置表单的 enctype 为 "multipart/form-data"。 2. 在后端的控制器中定义一个处理文件上传的方法,并使用 @RequestParam 注解来接收上传的图片文件。例如: ``` @RequestMapping("/upload") public String upload(@RequestParam("file") MultipartFile file) { // 处理上传的文件 return "redirect:/image"; // 重定向到显示图片的方法 } ``` 3. 在处理上传的方法中可以进行文件的存储操作,例如将文件保存到本地的文件系统或将文件存储到数据库。 4. 在显示图片的方法中,可以通过文件的路径或存储的唯一标识来获取图片,并将图片返给前端页面。例如: ``` @RequestMapping("/image") public void showImage(HttpServletResponse response) throws IOException { // 从存储位置获取图片,如从文件系统获取或从数据库获取 File imageFile = new File("path/to/image.jpg"); // 设置响应的 Content-Type 为 image/jpeg response.setContentType(MediaType.IMAGE_JPEG_VALUE); // 将图片写入响应的输出流中 FileInputStream fileInputStream = new FileInputStream(imageFile); IOUtils.copy(fileInputStream, response.getOutputStream()); fileInputStream.close(); response.getOutputStream().close(); } ``` 5. 在前端页面中,可以使用 img 标签来显示图片,设置其 src 属性为显示图片的方法的 URL。例如: ``` <img src="/image" alt="Image"> ``` 通过以上步骤,我们可以实现图片上传回显的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值