java如何将文件上传至天翼云盘

java如何将文件上传至天翼云盘,第1张

使用Java编程可以将文件上传到夭翼云盘。步骤如下:

1.使用FileInputStream类建立文件输入流,以便从本地文件系统读取文件;

2.使用URL类建立HTTP连接,设置HTTP连接属性,包括访问权限等;

3.使用URLConnection类建立HTTP请求,将文件数据写入输出流;

4.关闭HTTP连接,使文件上传至夭翼云盘。

@Controller

public class UploadController extends BaseController {

private static final Log log = LogFactory.getLog(UploadController.class)

private UploadService uploadService

private AuthService authService

/**

* 大文件分成小文件块上传,一次传递一块,最后一块上传成功后,将合并所有已经上传的块,保存到File Server

* 上相应的位置,并返回已经成功上传的文件的详细属性. 当最后一块上传完毕,返回上传成功的信息。此时用getFileList查询该文件,

* 该文件的uploadStatus为2。client请自行处理该状态下文件如何显示。(for UPS Server)

*

*/

@RequestMapping("/core/v1/file/upload")

@ResponseBody

public Object upload(HttpServletResponse response,

@RequestParam(value = "client_id", required = false) String appkey,

@RequestParam(value = "sig", required = false) String appsig,

@RequestParam(value = "token", required = false) String token,

@RequestParam(value = "uuid", required = false) String uuid,

@RequestParam(value = "block", required = false) String blockIndex,

@RequestParam(value = "file", required = false) MultipartFile multipartFile,

@RequestParam Map<String, String>parameters) {

checkEmpty(appkey, BaseException.ERROR_CODE_16002)

checkEmpty(token, BaseException.ERROR_CODE_16007)

checkEmpty(uuid, BaseException.ERROR_CODE_20016)

checkEmpty(blockIndex, BaseException.ERROR_CODE_20006)

checkEmpty(appsig, BaseException.ERROR_CODE_10010)

if (multipartFile == null) {

throw new BaseException(BaseException.ERROR_CODE_20020)// 上传文件不存在

}

Long uuidL = parseLong(uuid, BaseException.ERROR_CODE_20016)

Integer blockIndexI = parseInt(blockIndex, BaseException.ERROR_CODE_20006)

Map<String, Object>appMap = getAuthService().validateSigature(parameters)

AccessToken accessToken = CasUtil.checkAccessToken(token, appMap)

Long uid = accessToken.getUid()

String bucketUrl = accessToken.getBucketUrl()

// 从上传目录拷贝文件到工作目录

String fileAbsulutePath = null

try {

fileAbsulutePath = this.copyFile(multipartFile.getInputStream(), multipartFile.getOriginalFilename())

} catch (IOException ioe) {

log.error(ioe.getMessage(), ioe)

throw new BaseException(BaseException.ERROR_CODE_20020)// 上传文件不存在

}

File uploadedFile = new File(Global.UPLOAD_TEMP_DIR + fileAbsulutePath)

checkEmptyFile(uploadedFile)// file 非空验证

Object rs = uploadService.upload(uuidL, blockIndexI, uid, uploadedFile, bucketUrl)

setHttpStatusOk(response)

return rs

}

// TODO 查看下这里是否有问题

// 上传文件非空验证

private void checkEmptyFile(File file) {

if (file == null || file.getAbsolutePath() == null) {

throw new BaseException(BaseException.ERROR_CODE_20020)// 上传文件不存在

}

}

/**

* 写文件到本地文件夹

*

* @throws IOException

* 返回生成的文件名

*/

private String copyFile(InputStream inputStream, String fileName) {

OutputStream outputStream = null

String tempFileName = null

int pointPosition = fileName.lastIndexOf(".")

if (pointPosition <0) {// myvedio

tempFileName = UUID.randomUUID().toString()// 94d1d2e0-9aad-4dd8-a0f6-494b0099ff26

} else {// myvedio.flv

tempFileName = UUID.randomUUID() + fileName.substring(pointPosition)// 94d1d2e0-9aad-4dd8-a0f6-494b0099ff26.flv

}

try {

outputStream = new FileOutputStream(Global.UPLOAD_TEMP_DIR + tempFileName)

int readBytes = 0

byte[] buffer = new byte[10000]

while ((readBytes = inputStream.read(buffer, 0, 10000)) != -1) {

outputStream.write(buffer, 0, readBytes)

}

return tempFileName

} catch (IOException ioe) {

// log.error(ioe.getMessage(), ioe)

throw new BaseException(BaseException.ERROR_CODE_20020)// 上传文件不存在

} finally {

if (outputStream != null) {

try {

outputStream.close()

} catch (IOException e) {

}

}

if (inputStream != null) {

try {

inputStream.close()

} catch (IOException e) {

}

}

}

}

/**

* 测试此服务是否可用

*

* @param response

* @return

* @author zwq7978

*/

@RequestMapping("/core/v1/file/testServer")

@ResponseBody

public Object testServer(HttpServletResponse response) {

setHttpStatusOk(response)

return Global.SUCCESS_RESPONSE

}

public UploadService getUploadService() {

return uploadService

}

public void setUploadService(UploadService uploadService) {

this.uploadService = uploadService

}

public void setAuthService(AuthService authService) {

this.authService = authService

}

public AuthService getAuthService() {

return authService

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/7915477.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-11
下一篇 2023-04-11

发表评论

登录后才能评论

评论列表(0条)

保存