一:文件上传
首先创建本地数据库存储Ossfile ossfile = new Ossfile();
// 获取单个文件
MultipartFile mf = entity.getValue();
// 获得原始文件名
fileName = mf.getOriginalFilename();
String[] split = fileName.split(".");
String type = fileName.substring(fileName.lastIndexOf(".") + 1);
byte[] bytes = mf.getBytes();
//上传文件至oss
String osskey = StringUtil.getUUID();
String objectName = SysConstant.OSS_FILE_UPLOAD_PATH + "/" + SysConstant.FY_FILE_UPLOAD_PATH + "/" + osskey;//创建文件的路径和名称
OSS ossClient = null;
try {
// 创建OSSClient实例。
ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
三个参数为阿里云密钥
// 上传文件。
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));
//bucketName为oss桶的名称
//objectName为文件的路径和名称
return true;
} catch (Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer, true));
StringBuffer logStr = new StringBuffer("\r\n\t").append("上传文件失败");
logStr.append("\r\n\t").append("key:").append(key);
logStr.append("\r\n\t").append("执行错误结果如下:");
logStr.append("\r\n\t").append(writer.toString());
LogEntity logEntity = new LogEntity("putObject", "com.datanew.util.AliyunOssHelper2", logStr.toString(), "error");
logEntity.putParams("key", key).putParams("bytes.length", bytes.length);
logEntity.setExtendMap(true);
LogUtil.saveLog(logEntity);
return false;
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
ossfile.setFiletype(type);
ossfile.setFilesize("" + mf.getSize());
ossfile.setFilename(fileName);
ossfile.setOsskey(osskey);
ossfile.setCreatedate(date);
保存ossfile,ossfile为数据库保存上传文件的路径名称大小等
二:文件下载
String osskey = (String) fileMap.get("osskey");
String name = SysConstant.OSS_FILE_UPLOAD_PATH + "/" + SysConstant.FY_FILE_UPLOAD_PATH + "/" + osskey
得到文件的名称
下载文件的方法实体,得到一个字节流
{
OSS ossClient = null;
InputStream inputStream = null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
// 创建OSSClient实例。
ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//三个参数为阿里云密钥
// Object是否存在
if (!ossClient.doesObjectExist(bucketName, name)) {
return null;
}
//bucketName为桶的名称
//name为文件的名称和路径
// 获取Object
OSSObject ossObject = ossClient.getObject(bucketName, key);
inputStream = ossObject.getObjectContent();
int ch;
while ((ch = inputStream.read()) != -1) {
outputStream.write(ch);
}
return outputStream;
} catch (Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer, true));
StringBuffer logStr = new StringBuffer("\r\n\t").append("下载文件失败");
logStr.append("\r\n\t").append("key:").append(key);
logStr.append("\r\n\t").append("执行错误结果如下:");
logStr.append("\r\n\t").append(writer.toString());
LogEntity logEntity = new LogEntity("getFile", "com.datanew.util.AliyunOssHelper2", logStr.toString(), "error");
logEntity.putParams("key", key);
logEntity.setExtendMap(true);
LogUtil.saveLog(logEntity);
return null;
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ossClient != null) {
ossClient.shutdown();
}
}
}--------下载方法结束
ByteArrayOutputStream baos = (ByteArrayOutputStream) AliyunOssHelper2.getFile(SysConstant.OSS_FILE_UPLOAD_PATH + "/" + SysConstant.FY_FILE_UPLOAD_PATH + "/" + osskey);
得到文件内容
返回前端
if (baos == null) {
throw new BusinessException("文件不存在");
}
byte[] content = baos.toByteArray();
String fileName = (String) fileMap.get("filename");
response.reset();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", (new StringBuilder("attachment; filename=\""))
.append(new String(fileName.getBytes("utf-8"),"ISO-8859-1")).append("\"").toString());
OutputStream os = response.getOutputStream();
os.write(content);
os.flush();
os.close();