栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot+jersey跨域文件上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot+jersey跨域文件上传

1.修改修改pom.xml文件

在pom.xml文件中添加代码


        
        
            commons-fileupload
            commons-fileupload
            1.3.1
        
        
        
        
            com.sun.jersey
            jersey-client
            1.19
        
        
            com.sun.jersey
            jersey-core
            1.19
        

2.在配置文件utils(自己创建的包文件夹)目录下添加文件

2.1 FileInfo

package com.rz.rz_cms.server.main.utils;
 
public class FileInfo {
 
	private String fileName;
	private boolean isSecret;
	private String absolutPath;
	private String relativePath;
 
	public String getFileName() {
		return fileName;
	}
 
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
 
	public String getAbsolutPath() {
		return absolutPath;
	}
 
	public void setAbsolutPath(String absolutPath) {
		this.absolutPath = absolutPath;
	}
 
	public String getRelativePath() {
		return relativePath;
	}
 
	public void setRelativePath(String relativePath) {
		this.relativePath = relativePath;
	}
 
	public boolean isSecret() {
		return isSecret;
	}
 
	public void setSecret(boolean isSecret) {
		this.isSecret = isSecret;
	}
 
	public FileInfo() {
	}
 
	public FileInfo(String fileName, boolean isSecret, String absolutPath, String relativePath) {
		super();
		this.fileName = fileName;
		this.isSecret = isSecret;
		this.absolutPath = absolutPath;
		this.relativePath = relativePath;
	}
 
	@Override
	public String toString() {
		return "FileInfo [fileName=" + fileName + ", isSecret=" + isSecret + ", absolutPath=" + absolutPath
				+ ", relativePath=" + relativePath + "]";
	}
 
}

2.2JesyFileUploadUtil

package com.rz.rz_cms.server.main.utils;
 
import java.io.IOException;
import java.util.Date;
import java.util.Random;
 
import javax.net.ssl.SSLContext;
 
//mport org.glassfish.jersey.SslConfigurator;
import org.springframework.web.multipart.MultipartFile;
 
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
//import com.sun.jersey.api.client.config.ClientConfig;
//import com.sun.jersey.api.client.config.DefaultClientConfig;
//import com.sun.jersey.client.urlconnection.HTTPSProperties;
 
 
 

public class JesyFileUploadUtil {
 
	
	public static FileInfo uploadFile(MultipartFile file, String serverUrl) throws IOException {
		//把request请求转换成多媒体的请求对象
//		MultipartHttpServletRequest mh = (MultipartHttpServletRequest) request;
		//根据文件名获取文件对象
//		MultipartFile cm = mh.getFile(fileName);
		//获取文件的上传流
		byte[] fbytes = file.getBytes();
		
		//重新设置文件名
		String newFileName = "";
		newFileName += new Date().getTime()+""; //将当前时间获得的毫秒数拼接到新的文件名上
		//随机生成一个3位的随机数
		Random r = new Random();
		for(int i=0; i<3; i++) {
			newFileName += r.nextInt(10); //生成一个0-10之间的随机整数
		}
		
		//获取文件的扩展名
		String orginalFilename = file.getOriginalFilename();
		String suffix = orginalFilename.substring(orginalFilename.indexOf("."));
		
		
		//创建jesy服务器,进行跨服务器上传
//		Client client = Client.create(getClientConfig());
		Client client = Client.create();
		//把文件关联到远程服务器
		//http://127.0.0.1:8080/ssm_image_server/upload/123131312321.jpg
		WebResource resource = client.resource(serverUrl+"/upload/"+newFileName+suffix);
		//上传
		resource.put(String.class, fbytes);
		
		//图片上传成功后要做的事儿
		//1、ajax回调函数做图片回显(需要图片的完整路径)
		//2、将图片的路径保存到数据库(需要图片的相对路径)
		String fullPath = serverUrl+"/upload/"+newFileName+suffix; //全路径
		String relativePath = "/upload/"+newFileName+suffix; //相对路径
		
//		//生成一个json响应给客户端
//		String resultJson = "{"fullPath":""+fullPath+"", "relativePath":""+relativePath+""}";
//		return resultJson;
		
		return new FileInfo(newFileName+suffix, false, fullPath, relativePath);
	}
	
	
//	@SuppressWarnings("unused")
//	private static ClientConfig getClientConfig() {
//		SslConfigurator sslConfig = SslConfigurator.newInstance();
//		sslConfig.trustStoreFile("src/main/resources/lvxingfw.jks")
//		.trustStorePassword("lxfw2021");
//		sslConfig.securityProtocol("SSL");
//    	SSLContext sslContext = sslConfig.createSSLContext();
//
//	    ClientConfig cc = new DefaultClientConfig();
//	    cc.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
//	    		new HTTPSProperties(new MyHostnameVerifier(), sslContext));
//
//	    return cc;
//	}
}
		String fullPath = serverUrl+"/upload/"+newFileName+suffix; //全路径
		String relativePath = "/upload/"+newFileName+suffix; //相对路径

3.Controller下添加FileController跨越文件

package com.rz.rz_cms.server.main.controller;
 
import com.rz.rz_cms.server.main.utils.FileInfo;
import com.rz.rz_cms.server.main.utils.HttpResult;
import com.rz.rz_cms.server.main.utils.JesyFileUploadUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.IOException;
 
@RestController
@RequestMapping("/upload")
public class FileController {
 
    @CrossOrigin(origins = "*")
    @PostMapping("/file")
    @ResponseBody
    public String upload(MultipartFile file){
        String result = null;
        try{
 
            FileInfo fileInfo = JesyFileUploadUtil.uploadFile(file,"http://localhost:8060");//访问路径可修改端口号
            result = fileInfo.getRelativePath();//获取上传文件时的路径
        }catch (IOException e){
            e.printStackTrace();
            result = null;
        }
 
        return result;
    }
}

4.在tomcat中操作

1.修改端口号

(1)打开你的tomcat的安装的路径,点击conf,找到server.xml打开

按住ctrl+f搜索8080,进行修改端口号

 2.修改上传资源

找到web.xml打开

按住ctrl+f搜索搜索DefaultServlet找到这个位置,然后按照图片红框标注进行添加修改

 3.运行tomcat

按照图片进行操作即可

 输入cmd

 在控制台输入 st  按TAB键补全然后 ENTER键运行即可

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/930561.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号