java 接受流文件上传_如何实现java 流式文件上传

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);

mapappmap = 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);// 上传文件不存在

}

}

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) {

}

}

}

}

@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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值