本文小编为大家详细介绍“怎么用springboot+jersey+tomcat实现跨域方式上传文件到服务器”,内容详细,步骤清晰,细节处理妥当,希望这篇“怎么用springboot+jersey+tomcat实现跨域方式上传文件到服务器”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
在服务器上,当我们启动了tomcat,就可以以
的方式,进行访问到我们服务器上处于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-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
上传文件代码
<!-- 跨域上传依赖--> <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; }
读到这里,这篇“怎么用springboot+jersey+tomcat实现跨域方式上传文件到服务器”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注编程之家行业资讯频道。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。