我们在前面已经学习如何把文件上传到远程服务器中,这里的远程服务器一般是我们自己的Linux服务器。
如下:
java上传文件到远程服务器(一)---HttpURLConnection方式
java上传文件到远程服务器(二)---HttpClient方式
如果说 不想自己搭建 静态资源Linux服务器的话,现在有很多免费的静态资源云服务可以使用。
本文记录java上传文件到远程服务器七牛云中。
关于七牛云的介绍和注册使用读者可以自己查看官网。
https://www.qiniu.com/
使用七牛云作为图片等静态资源存储的话 查看 对象存储功能。
在实现上传文件到七牛云之前需要准备三个东西。
String accessKey = "your access key"; // 个人中心密钥管理中的AK
String secretKey = "your secret key"; //个人中心密钥管理中的SK
String bucket = "your bucket name"; // 对象存储空间名(如果有多个空间,自己想要上传到哪个空间则使用哪个空间名)
如下图:
我们在之前的文章中已经在SpringMVC基础框架的基础上应用了BootStrap的后台框架,在此基础上记录HttpURLConnection上传文件到远程服务器。
基础项目源码下载地址为:
SpringMVC+Shiro+MongoDB+BootStrap基础框架
我们在基础项目中已经做好了首页index的访问。
现在就在index.jsp页面和index的路由Controller上做修改,上传文件到七牛云。
index.jsp代码为:
<%@ include file="./include/header.jsp"%>
HttpURLConnection HttpURLConnection
<%@ include file="./include/footer.jsp"%>