本文实例为大家分享了springMVC+jersey实现跨服务器文件上传的具体代码,供大家参考,具体内容如下
1.首先添加所需要的jar包
2.在springMVC的配置文件中添加文件上传解析器
3.jsp页面
4.文件上传的 js
5. controller
@RequestMapping("fileUpload")
public @ResponseBody Map fileUpload(HttpServletRequest request,String fileName){
System.out.println(111);
//1.将普通请求转换为多部件请求
MultipartHttpServletRequest mr = (MultipartHttpServletRequest)request;
//2.根据文件名获取文件对象
CommonsMultipartFile mf = (CommonsMultipartFile)mr.getFile(fileName);
//3.获取文件全名称
String originalFilename = mf.getOriginalFilename();
System.out.println("文件全名称"+originalFilename);
//4.获取后缀
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
System.out.println("后缀"+suffix);
//5.将文件对象转换为字节
byte[] fileBytes = mf.getBytes();
//6.获取新的随机文件名
String newFileName="";
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
int num = (int)(Math.random()*899)+100;
newFileName = sdf.format(new Date())+num;
System.out.println("新的随机文件名"+newFileName);
//开始上传
//1.创建jesy服务器
Client client = Client.create();
String fullPath = "http://localhost:8088/fileServiceProject/upload/"+newFileName+suffix;
//把文件关联到远程服务器
WebResource wr = client.resource(fullPath);
//2.相对路径
String reletivePath = "/upload/"+newFileName+suffix;
//3.上传
wr.put(String.class, fileBytes);
Map map = new HashMap();
map.put("fullPath", fullPath);
map.put("reletivePath", reletivePath);
return map;
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



