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

前言

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

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

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

如图:
在这里插入图片描述

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

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

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

准备工作

首先你得安装tomcat
在这里插入图片描述

安装完成后后启动

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

为了能够成功上传文件,需要放开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方法即可

结语

以上就是直接通过tomcat跨域上传文件到服务器的方式

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值