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

Springboot——跨服务器文件上传

Java 更新时间:发布时间: 百科书网 趣学号
一、文件目录


注释:

  1. Servers : Tomcat服务器(只要安装tomcat就会有)
  2. springboot : 服务器1. 服务器端口号改为8090(文件夹虽然爆红,但是不影响)
  3. uploadController : 控制结构
  4. JesyFileUploadUtil : 自定义的jesy工具类
  5. application.properties : 端口号修改
  6. ssm_image_server : 服务器2(因为要实现的效果是跨服务器文件上传,所以自己模拟了一个服务器,需要用tomcat启动,端口号未更改,还是8080.)
  7. upload : 承接上传文件的文件夹。
  8. webapp代码,在我的资源里面,直接粘贴即可。(我的本地在D盘eclip文件夹下wedapp.zip)
二、代码

demo包下的controller包下的uploadController

package demo.controller;

import java.io.IOException;

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.bind.annotation.ResponseBody;

import demo.utils.JesyFileUploadUtil;

@Controller
public class uploadController {
	
	private final String PIC_URL = "http://127.0.0.1:8080/ssm_image_server";

	@RequestMapping(value = "uploadPic" , method = RequestMethod.POST)
	@ResponseBody
	public String uploadPic(HttpServletRequest request , String fileName){
		//调用跨服务器上传文件的工具类方法上传文件
		
		String result = "";
		try {
			result = JesyFileUploadUtil.uploadFile(request, fileName, PIC_URL);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
}

工具类:util包下的JesyFileUploadUtil

package demo.utils;

import java.io.IOException;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

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


public class JesyFileUploadUtil {

	
	public static String uploadFile(HttpServletRequest request,String fileName,String servlerUrl) throws IOException{
		//把request对象转换成多媒体请求对象
		MultipartHttpServletRequest mh = (MultipartHttpServletRequest)request;
		//根据文件名获取文件对象
		MultipartFile cm = mh.getFile(fileName);
		//获取文件的上传流
		byte[] fbytes = cm.getBytes();
		
		
		//重新设置文件名,避免相同
		String newFileName = "";
		//生成毫秒数,将当前时间生成的毫秒数拼接到文件名上
		newFileName += new Date().getTime()+"";
		//生成三位的随机数
		Random r = new Random();
		for(int i = 0 ; i<3 ; i++){
			//生成一个0到10之间的三位随机整数
			newFileName += r.nextInt(10);
		}
		
		
		//获取上传文件的扩展名
		String orginalFilename = cm.getOriginalFilename();
		String suffix = orginalFilename.substring(orginalFilename.indexOf("."));
	
		
		//创建一个jesy服务器,进行跨服务器上传
		Client client = Client.create();
		//把文件关联到远程服务器
		WebResource resourse = client.resource(servlerUrl+"/upload/"+newFileName+suffix);
		//上传
		resourse.put(String.class,fbytes);
		
		//图片上传成功后要做的事情
		
		String fullPath = servlerUrl+"/upload/"+newFileName+suffix;
		String relativePath = "/upload/"+newFileName+suffix;//相对路径
		
		//生成一个json响应给客户端
		String resultJson = "{"fullPath":""+fullPath+"", "relativePath":""+relativePath+""}";
		return resultJson;
	}
}

主方法

package demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MainApplication {

	public static void main(String[] args) {
		SpringApplication.run(MainApplication.class, args);

	}

}

端口号更改

server.port=8090

springboot / pom.xml文件


	4.0.0
	
	
		org.springframework.boot
		spring-boot-starter-parent
		1.5.2.RELEASE
	
	
	
	
	fan
	springboot
	0.0.1-SNAPSHOT
	jar
 
	
		
			org.springframework
			spring-webmvc
		
		
		
			com.jolbox
			bonecp-spring
			0.8.0.RELEASE
		
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
		
		
			org.apache.tomcat.embed
			tomcat-embed-jasper
			provided
		
		
		
		
		   commons-fileupload
		   commons-fileupload
		   1.3.1
		
		
		
		   com.sun.jersey
		   jersey-client
		   1.19
		
		
		
		   com.sun.jersey
		   jersey-core
		   1.19
		
	
	
		${project.artifactId}
		
		
			
       			 org.apache.maven.plugins
       			 maven-war-plugin
       			 3.3.1
    	 	 
    	  
			
			
				org.apache.maven.plugins
				maven-resources-plugin
				
					UTF-8
				
			
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				
					1.7
					1.7
					UTF-8
				
			
			
			
				org.springframework.boot
				spring-boot-maven-plugin
			
			
		
		
			
				
				
					org.apache.tomcat.maven
					tomcat7-maven-plugin
					2.2
				
			
		
	

ssm_image_server为一个动态的web项目,new——填写文件名——finish即可。ssm_image_server项目中没有代码,只建立了一个名为upload的文件夹。具体路径见项目目录

三、运行

不同于一般的文件运行,因为想在一台服务器上模拟跨服务器文件上传,所以对springboot进行了端口号修改,springboot为8090。ssm_image_server为8080。

运行ssm_image_server

出现如下图字样即运行成功。

springboot采用主程序运行方式

找到你所编写的main方法。我写的主程序为MainApplication,右键——run as —— java application。

出现如下图字样即为运行成功。

目前两个服务器同时运行:

网址输入:http://127.0.0.1:8090/jsps/index.jsp
效果如图所示

点击选择文件即可实现,文件上传。(此程序以图片为例)

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

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号