[英]File not launching in Client Side Java (Server/Client) App no Web
問題:服務器請求我的文件無法啟動。
好的,我已經編寫了一個服務器/客戶端應用程序,但是問題是當我從服務器請求文件傳輸到客戶端時,但是我注意到,我需要手動刷新目錄才能將文件保存在路徑或目錄。 如此說來,我覺得這就是為什么我的代碼在請求文件不啟動時的原因。
我的方法是在剛被請求之后從客戶端啟動文件。
這是我的代碼如下:
public static void receiveFile(String fileName) {
try {
int bytesRead;
InputStream in = sock.getInputStream();
DataInputStream clientData = new DataInputStream(in);
fileName = clientData.readUTF();
OutputStream output = new FileOutputStream((fileName));//need to state a repository
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
output.write(buffer, 0, bytesRead);
size -= bytesRead;
}
output.close();
in.close();
File file = new File(fileName);
Desktop.getDesktop().open(file);
//System.out.println("File "+fileName+" received from Server.");
} catch (IOException ex) {
//Logger.getLogger(CLIENTConnection.class.getName()).log(Level.SEVERE, null, ex);
ex.printStackTrace();
}
}
請您看看是不是告訴我您認為我做錯了什么? 服務器代碼:
關閉前,請嘗試沖洗流。
output.flush();
output.close();
我自己解決了問題,答案是使用下面的代碼:
替換為:Desktop.getDesktop()。open(file);
使用:Runtime.getRuntime()。exec(“ rundll32 url.dll,FileProtocolHandler” + file);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.