
注释:
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
效果如图所示
点击选择文件即可实现,文件上传。(此程序以图片为例)