美文网首页Hello Java
上传文件到应用服务器临时路径

上传文件到应用服务器临时路径

作者: Aldeo | 来源:发表于2018-04-28 14:48 被阅读12次

    public static String uploadToTmp(MultipartFile file) throws IOException {
    //时间戳
    String tim = String.valueOf ( System.currentTimeMillis () );
    //临时路径存储临时文件
    String tmpPath = "/fileUpload/tmp/"+tim+"/";
    File tmpdir = new File ( tmpPath );
    if (!tmpdir.exists ()) {
    tmpdir.mkdirs ();
    }
    //临时文件的全路径
    String tmpUrl = tmpPath + file.getOriginalFilename();
    File tmptFile = new File ( tmpUrl );

        FileOutputStream fs=new FileOutputStream(tmptFile);
        InputStream stream = file.getInputStream();
        byte[] buffer =new byte[1024*1024];
        int bytesum = 0;
        int byteread = 0;
        while ((byteread=stream.read(buffer))!=-1)
        {
            bytesum+=byteread;
            fs.write(buffer,0,byteread);
            fs.flush();
        }
        fs.close();
        stream.close();
    
        return tmpUrl;
    }

    相关文章

      网友评论

        本文标题:上传文件到应用服务器临时路径

        本文链接:https://www.haomeiwen.com/subject/kgyglftx.html