下午好:我需要將音頻文件(.wav)從客戶端傳輸到服務器。問題是我的代碼運行時沒有錯誤,但文件沒有被傳輸,因爲目標文件夾沒有出現!我希望有人能幫助我..問候!將文件上傳到服務器
Código:
try {
URL pagina = new URL("http://localhost:8081/prueba/audio.wav");
HttpURLConnection con = (HttpURLConnection) pagina.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
con.setRequestMethod("POST");
con.setRequestProperty("content-type", "audio/x-wav");
con.setUseCaches(false);
con.connect();
String filename = System.getProperty("user.home") + "\\.vmaudio\\" + "audio.wav";
FileInputStream is = new FileInputStream(filename);
DataOutputStream fos = new DataOutputStream(con.getOutputStream());
int fByte;
int bytesTrasferidos = 0;
while ((fByte = is.read()) != -1) {
fos.write(fByte);
fos.flush();
bytesTrasferidos++;
}
System.out.println("Bytes Transferidos: "+bytesTrasferidos);
fos.close();
is.close();
con.disconnect();
} catch (MalformedURLException ex) {
Logger.getLogger(pruebasVarias.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(pruebasVarias.class.getName()).log(Level.SEVERE, null, ex);
}
PD:可能需要創建一個接收文件一個servlet,並將它們複製到該文件夾在服務器上,但事實是沒有,我的想法是直接從客戶端發送..
感謝您的信息! – DavidH