我有這個問題。 我使用spring,並將附件(doc,pdf,png ...)存儲到服務器文件系統中。 然後我將文件的路徑和名稱保存到我的數據庫中。 現在我怎樣才能讀取這個文件作爲鏈接到瀏覽器?從java文件夾文件系統輸出存儲的文件
我以爲寫文件到網站的位置,並給這個位置的瀏覽器。 這是一個很好的做法嗎? 但是,如何在可視化後刪除文件?
我希望問題很清楚。
寫我用:
/** what write for reach temp-file folder (my project name is intranet)
I thougth TEMP_FOLDER=""/intranet/resources/temp-files/";
but It doesnt work. ioexception (The system cannot find the path specified)
*/
final static String TEMP_FOLDER=?????
public static String createTempFile(String originalPathFile,String fileName){
String tempPathFile="";
try {
InputStream inputStream = new FileInputStream(originalPathFile);
tempPathFile=TEMP_FOLDER+fileName;
File tempFile = new File(tempPathFile);
OutputStream out = new FileOutputStream(tempFile);
int read = 0;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException ioe) {
System.out.println("Error while Creating File in Java" + ioe);
}
return tempPathFile;
}
是!!!!太好了!!!謝謝!... – Shinigami