第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在`java.nio.file.Path`中用forwardSlash(/)替換

如何在`java.nio.file.Path`中用forwardSlash(/)替換

MYYA 2023-11-01 21:44:22
我正在保存多部分文件,并且正在使用類Path。java.nio.file.Path在這個(gè)文件中,Path我得到了路徑C:\for\expample\,但我需要這樣的路徑C:/for/expample/。在這里,我分享我嘗試過的代碼,但不幸的是,我沒有得到帶有正斜杠的真實(shí)路徑。public String saveFile(MultipartFile theFile, String rootPath, String filePath , String fileNme) throws Exception {        try {            Path fPath = null;            if(theFile != null) {                Path path = Paths.get(rootPath, filePath);                if(Files.notExists(path)) {                    //Create directory if one does not exists                    Files.createDirectories(path);                }                String fileName;                //Create a new file at that location                if(fileNme == "") {                    fileName = theFile.getOriginalFilename();                }else {                    fileName = fileNme;                }                fPath = Paths.get(rootPath, filePath, fileName);                if(Files.isRegularFile(fPath) && Files.exists(fPath)) {                    Files.delete(fPath);                }                StringWriter writer = new StringWriter();                IOUtils.copy(theFile.getInputStream(), writer, StandardCharsets.UTF_8);                File newFile = new File(fPath.toString());                newFile.createNewFile();                try (OutputStream os = Files.newOutputStream(fPath)) {                    os.write(theFile.getBytes());                }            }            return this.replaceBackslashes(fPath == null ? "" :fPath.normalize().toString());        }catch (IOException e) {            e.printStackTrace();            throw new Exception("Error while storing the file");        }    }
查看完整描述

3 回答

?
慕的地6264312

TA貢獻(xiàn)1817條經(jīng)驗(yàn) 獲得超6個(gè)贊

嘗試

return fPath == null ? "" : fPath.normalize().toString().replace("\\","/");


查看完整回答
反對(duì) 回復(fù) 2023-11-01
?
狐的傳說

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超3個(gè)贊

給定一個(gè)Path具有 的對(duì)象C:\\aaaa\\bbbb,只需將所有雙黑斜杠替換為正斜杠

path.toString().replaceAll("\\\\", "/");

輸出:C:/aaaa/bbbb


查看完整回答
反對(duì) 回復(fù) 2023-11-01
?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

將完整路徑轉(zhuǎn)換為字符串并使用正則表達(dá)式,例如

String str = fPath.toString();
str = str.replace("\\", "/");


查看完整回答
反對(duì) 回復(fù) 2023-11-01
  • 3 回答
  • 0 關(guān)注
  • 187 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)