SpringMVC第10讲:跨服务器文件上传

源代码:

https://download.csdn.net/download/qzc70919700/19929653

一、导入jar包

二、项目编码

2.1、项目需求

上传图片,图片里面回显。Ajax。页面不刷新图片回显

2.2、模拟两台服务器

创建一个项目,图片服务器项目,图片服务器和上传图片的项目端口不一致。

2.3、配置springmvc.xml文件支持文件上传类

<!-- 文件上传解析器 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10240000"></property>
</bean>

2.3、前台页面ajax发送请求

发送请求,上传图片:图片被关联表单。提交表单:jquery.form.js

JavaScript:

<script type="text/javascript">
function submitImgSize1Upload(){
    var option={
	type:'POST',
	url:'${pageContext.request.contextPath }/upload/uploadPic.do',
	dataType:'text',
	data:{
		fileName : 'imgSize1File'
	},
	success:function(data){
				
		//把json格式的字符串转换成json对象
		var jsonObj = $.parseJSON(data);
				
		//返回服务器图片路径,把图片路径设置给img标签
		$("#imgSize1ImgSrc").attr("src",jsonObj.fullPath);
		//数据库保存相对路径
		$("#imgSize1").val(jsonObj.relativePath);
	}
    };
    $("#itemForm").ajaxSubmit(option);
}
</script>

HTML:

<form id="itemForm" method="post">
	<img id='imgSize1ImgSrc' src='${picPath }${item.pic }' height="100" width="100" />
	<input type='file' id='imgSize1File' name='imgSize1File' class="file" onchange='submitImgSize1Upload()' />
</form>

UploadController.java

package demo.mvc.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

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

@Controller
@RequestMapping("/upload")
public class UploadController {
	
	private final String PIC_HOST = "http://127.0.0.1:8083/ssm_image_server";

	@RequestMapping(value = "uploadPic", method = RequestMethod.POST)
	public void uploadPic(HttpServletRequest request,String fileName,PrintWriter out) {
		//把request请求强转成多部件请求对象
		MultipartHttpServletRequest mh = (MultipartHttpServletRequest) request;
		//根据文件名获取文件对象
		CommonsMultipartFile cm = (CommonsMultipartFile) mh.getFile(fileName);
		//获取文件上传流
		byte[] fbytes = cm.getBytes();
		
		//重新设置文件名
		String newFileName = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		newFileName = sdf.format(new Date());
		
		Random r = new Random();
		for(int i=0; i<3; i++) {
			newFileName = newFileName + r.nextInt(10);
		}
		
		//获取文件扩展名
		String originalFilename = cm.getOriginalFilename();
		String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
		
		//创建jesy服务器,进行跨服务器上传
		Client client = Client.create();
		//把文件关联到远程服务器
		WebResource resource = client.resource(PIC_HOST + "/upload/" + newFileName + suffix);
		//上传
		resource.put(String.class, fbytes);
		
		
		//ajax回调函数需要会写什么东西?
		//图片需要回显:需要图片完整路径
		//数据库保存图片的相对路径.
		String fullPath = PIC_HOST + "/upload/" + newFileName + suffix;
		String relativePath="/upload/" + newFileName + suffix;
		//{"":"","":""}
		String result="{\"fullPath\":\"" + fullPath+"\",\"relativePath\":\"" + relativePath +"\"}";
		
		out.print(result);
	}
}

2.4、修改图片服务器文件上传权限

2.5、运行结果

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值