软件编程
位置:首页>> 软件编程>> java编程>> springboot+jersey+tomcat实现跨域方式上传文件到服务器的方式

springboot+jersey+tomcat实现跨域方式上传文件到服务器的方式

作者:相与还  发布时间:2023-08-16 10:26:45 

标签:springboot,jersey,tomcat,跨域,上传,服务器

前言

在服务器上,当我们启动了tomcat,就可以以

http://ip地址:8080/文件路径/文件名

的方式,进行访问到我们服务器上处于tomcat的webapps文件夹下的文件

如图:

springboot+jersey+tomcat实现跨域方式上传文件到服务器的方式

上面我是用的http://47.92.53.108:8080/IMG/img04.jpg进行访问文件

于是为了可以往上面加文件,我们有两种方式,一种就是直接复制文件到路径上,

另一种自然是通过代码的方式,调用接口往上面上传文件

准备工作

首先你得安装tomcat

springboot+jersey+tomcat实现跨域方式上传文件到服务器的方式

安装完成后后启动

然后,需要注意的是,为了让我们能够访问文件,那么我们需要做这么一件事,开放服务器的安全策略
把端口8080放开

springboot+jersey+tomcat实现跨域方式上传文件到服务器的方式

为了能够成功上传文件,需要放开tomcat的写权限,
即解决报错returned a response status of 405 Method Not Allowed

在tomcat的conf文件夹,找到web.xml文件,添加如下代码

<!-- 使得服务器允许文件写入。-->
<init-param>
           <param-name>readonly</param-name>
           <param-value>false</param-value>
       </init-param>

注意,该代码需要在servlet标签内部添加,即:

<servlet>
       <servlet-name>default</servlet-name>
       <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
       <init-param>
           <param-name>debug</param-name>
           <param-value>0</param-value>
       </init-param>
       <init-param>
           <param-name>listings</param-name>
           <param-value>false</param-value>
       </init-param>
       <!-- 使得服务器允许文件写入。-->
<init-param>
           <param-name>readonly</param-name>
           <param-value>false</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
   </servlet>

加完代码记得重启tomcat

上传文件代码

在pom.xml文件加入代码:

<!--        跨域上传依赖-->
       <dependency>
           <groupId>com.sun.jersey</groupId>
           <artifactId>jersey-core</artifactId>
           <version>1.18.1</version>
       </dependency>
       <dependency>
           <groupId>com.sun.jersey</groupId>
           <artifactId>jersey-client</artifactId>
           <version>1.18.1</version>
       </dependency>
@PostMapping("/upLoadImg")
   @ResponseBody
   public String upLoadImg(MultipartFile myfile){

String path = "http://服务器公网ip:8080/tomcat的webapps下的文件夹名称/";

//为上传到服务器的文件取名,使用UUID防止文件名重复
       String type= myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
       String filename= UUID.randomUUID().toString()+type;
       try{
//使用Jersey客户端上传文件
           Client client = Client.create();
           WebResource webResource = client.resource(path +"/" + URLEncoder.encode(filename,"utf-8"));
           webResource.put(myfile.getBytes());
           System.out.println("上传成功");
           System.out.println("图片路径==》"+path+filename);
       }catch(Exception ex){
           System.out.println("上传失败");
       }
       return "上传成功";
   }

以上会随机生成uuid作为文件名
如果想保留原本文件名称,参考如下代码
有一个需要注意的是:如果以原文件名命名进行上传,文件名不能包含中文
否则会报错400

@PostMapping("/upLoadImg")
   @ResponseBody
   public String doRemoteUpload(@RequestParam("file")MultipartFile file){
       String path = "http://服务器公网ip:8080/tomcat的webapps下的文件夹名称/";
       String filename= file.getOriginalFilename();
       try{
           Client client = Client.create();
           WebResource webResource = client.resource(path +"/" + filename);
           webResource.put(file.getBytes());
       }catch(Exception ex){
           return "上传文件失败:"+path+"/"+filename;
       }
       return "上传文件成功:"+path+"/"+filename;
   }

导入的import为:

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

删除服务器文件

@GetMapping("/deleteUploadImg")
   @ResponseBody
   public ResultVO deleteUploadImg(){

String path = "http://服务器公网ip:8080/tomcat的webapps下的文件夹名称/文件名";
       try{
           Client client = Client.create();
           WebResource webResource = client.resource(path);
           webResource.delete();
       }catch(Exception ex){
           return "删除文件失败:"+path+"/"+filename+ ex.getMessage();
       }
       return "删除文件成功:"+path+"/"+filename;
   }

如果需要 删除文件

只需要把文件的路径传入
并且使用WebResourcedelete方法即可

来源:https://blog.csdn.net/xc9711/article/details/129215577

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com