下午好:我需要将音频文件(.wav)从客户端传输到服务器。问题是我的代码运行没有错误,但文件没有传输,因为目标文件夹中没有出现!我希望有人可以帮助我.. 问候!
科迪戈:
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 来接收文件并将它们复制到服务器上的文件夹中,但事实并非如此,我的想法是直接从客户端发送它..