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

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;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容