檔案上傳到本地磁碟,磁碟路徑適應window和linux
private void uploadDisk(InputStream inputStream, String fileName) {
OutputStream os = null;
try {
String csvDir = "/data/csv";
File csvDirectory = new File(csvDir);
if (!csvDirectory.exists()) {
if (!csvDirectory.mkdirs()) {
throw new RuntimeException("can't make csv directory:[" + csvDir + "]");
}
}
if (!csvDirectory.canWrite() && !csvDirectory.canRead()) {
throw new RuntimeException("no read/write right to target csv directory:[" + csvDir + "]!");
}
String csvFile = String.join(File.separator, new String[] { csvDirectory.getAbsolutePath(), fileName});
byte[] bs = new byte[1024];
int len;
os = new FileOutputStream(csvFile);
while ((len = inputStream.read(bs)) != -1) {
os.write(bs, 0, len);
}
} catch (IOException e){
e.printStackTrace();
} finally {
try {
os.close();
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}