程序员大体有两种出路:一、在某一领域深挖成为架构师,二、尽可能掌握各个端的知识,扩展知识宽度,成为项目经理.
困扰了我一阵子的文件服务今天终于搞定了,进行一个记录.
一、环境
1、前端:vue+element
2、OSS:阿里云
3、后端:Java
二、具体步骤
1、前端:
使用文件上传组件选择文件:action:http://localhost:8712/file/file/fileUpload为上传地址
<el-upload
class="upload-demo"
action="http://localhost:8712/file/file/fileUpload"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-remove="beforeRemove"
multiple
:limit="13"
:on-exceed="handleExceed"
:file-list="fileList"
>
<el-button size="small" type="primary">点击上传</el-button>
<div slot="tip" class="el-upload__tip">
只能上传jpg/png文件,且不超过500kb
</div>
</el-upload>
fileList
fileList: [
{
name: 'food.jpeg',
url:
'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
},
{
name: 'food2.jpeg',
url:
'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
}
]
2、后端:
(1)、创建文件接收方法:
文件接收方式为:MultipartFile[],接收到的文件先用ByteArrayInputStream方法转换为InputStream形式
/*
* 上传文件到OSS
* 返回设置结果(json)
* @Param JSONObject jsonObject
*/
@RequestMapping(value="/file/fileUpload",method= RequestMethod.POST)
@ApiOperation(value="上传文件到OSS", notes="test")
public String fileUpload(MultipartFile[] file, HttpServletRequest request)throws Exception {
JSONObject json =new JSONObject();
List fileRes =new ArrayList<>();
for (int i =0; i < file.length; i++) {
MultipartFile filea = file[i];
byte [] byteArr=filea.getBytes();
InputStream inputStream =new ByteArrayInputStream(byteArr);
HashMap b =OSSUtil.uploadFile(filea.getOriginalFilename(), inputStream);
fileRes.add(b);
}
json.put("code",200);
json.put("msg","成功");
json.put("data", fileRes);
return json.toString();
}
(2)、构建OSS文件上传服务
所需准备的内容:
endpoint(固定)、accessKeyId(下图)、accessKeySecret(下图)、bucketName
代码如下:
public class OSSutil {
// static Logger logger = Logger;
private static Stringendpoint ="https://oss-cn-beijing.aliyuncs.com";//自己的服务器地址
// accessKeyId和accessKeySecret是OSS的访问密钥
private static StringaccessKeyId ="LTAI3vRy1U84f";//假的
private static StringaccessKeySecret ="kr4YCObgS6igwvXP";//假的
// Bucket用来管理所存储Object的存储空间
private static StringbucketName ="n";
/**
* 上传文件
* @param filename 上传文件名
* @param input 上传文件流
* @return
*/
public static HashMap uploadFile(String filename, InputStream input) {
boolean success =false;
OSSClient ossClient =new OSSClient(endpoint,accessKeyId,accessKeySecret);
try {
if (ossClient.doesBucketExist(bucketName)) {
System.out.print("您已经创建Bucket:" +bucketName +"。");
}else {
System.out.print("您的Bucket不存在,创建Bucket:" +bucketName +"。");
ossClient.createBucket(bucketName);
}
PutObjectResult res = ossClient.putObject(bucketName,filename,input);
success =true;
ossClient.shutdown();
}catch (OSSException oe) {
oe.printStackTrace();
}catch (ClientException ce) {
ce.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}finally {
ossClient.shutdown();
}
HashMap fileResult =new HashMap();
fileResult.put("status", success);
fileResult.put("name", filename);
fileResult.put("url","https://" +bucketName +".oss-cn-beijing.aliyuncs.com/" + filename);
return fileResult;
}
}
三、成功:
网友评论