GithubHelp home page GithubHelp logo

Comments (2)

0312birdzhang avatar 0312birdzhang commented on August 19, 2024

已解决,用modify_file1,第一次先上传一个文件,后面逐渐用偏移添加

from fastdfs-client-java.

0312birdzhang avatar 0312birdzhang commented on August 19, 2024

贴一下主要的代码吧,或许会有需要的

/**
     * 大文件分块上传
     * @author debo.zhang
     * @param in
     * @param upload
     * @param remoteid
     * @throws IOException
     * @throws NoSuchAlgorithmException 
     */
    public String bigBufferUpload(InputStream in,boolean upload,String remoteid,MessageDigest md) throws IOException, NoSuchAlgorithmException{
        byte[] buffer = new byte[1024];
        ByteArrayOutputStream ous = null;
        NameValuePair[] meta = null;
        long buffer_length = 0;
        boolean read_end = false;
        ous = new ByteArrayOutputStream();

        while(true){
            //一次400M上传,根据jvm虚拟机大小调整,不要设置太小导致频繁跟fastdfs交互
            if(buffer_length > 1024 * 1024 * 400){
                break;
            }
            int r = in.read(buffer);
            if(r == -1){
                read_end = true;
                break;
            }

            ous.write(buffer, 0, r);
            buffer_length += r;

            md.update(buffer, 0, r);
        }

        if(upload){
            Ret ret = FastDFSService.uploadAppendfile(ous.toByteArray(),filetype,meta);
            if(ret != ResultKit.fail()){
                remoteid = ret.get(Constant.DATA);
                upload = false;
                if(!read_end){
                    bigBufferUpload(in, false, remoteid,md);
                }else{
                    byte[] digest = md.digest();
                    filemd5 = new String(Hex.encodeHex(digest));
                }
            }
        }else{
            byte[] buff = ous.toByteArray();
            long filesize = FastDFSService.getFileInfo(remoteid).getFileSize();
            FastDFSService.modifyAppendFile(remoteid, filesize, buff, 0,buff.length);
            if(!read_end){
                bigBufferUpload(in, false, remoteid,md);
            }else {
                byte[] digest = md.digest();
                filemd5 = new String(Hex.encodeHex(digest));
            }
        }
        return remoteid;
    }

然后用下面代码调用

public void upload(){
        HttpServletRequest req = getRequest();
        boolean isMultipart = ServletFileUpload.isMultipartContent(req);
        JsonMessageBean msg = new JsonMessageBean();
        if(isMultipart){
            ServletFileUpload upload = new ServletFileUpload();
            try {
                FileItemIterator iter = upload.getItemIterator(req);
                while (iter.hasNext()) {
                    FileItemStream item = iter.next();
                    input  = item.openStream();
                    if(item.isFormField()){
                        String fileName = item.getFieldName();
                        String value = Streams.asString(input);
                        if("filename".equals(fileName)){
                            this.filename = value;
                        }else if("uri".equals(fileName)){
                            this.uri = value;
                        }else if("filetype".equals(fileName)){
                            this.filetype = value;
                        }else if ("appid".equals(fileName)) {
                            this.appid = value;
                        }else if ("appzone".equals(fileName)) {
                            this.appzone = value;
                        }else if ("appsecret".equals(fileName)) {
                            this.appsecret = value;
                        }
                    }
                    // Handle a multi-part MIME encoded file.
                    else {
                    MessageDigest md = MessageDigest.getInstance("MD5");
                    dfspath = bigBufferUpload(input, true, "",md);
                    System.out.println("calculated md5:"+filemd5);

from fastdfs-client-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.