Hello Java

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

2018-04-28  本文已影响12人  Aldeo

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;
}
上一篇 下一篇

猜你喜欢

热点阅读