我遇到了Java HttpURLConnection的问题。我正在从客户端向服务器发送音频文件(midi格式)。这部分工作得很好。之后我想在服务器端使用该文件来编写我所编写的算法。该算法分析midi文件的类型和播放乐器。在分析midi文件时,我得到了一个自编写类StringArrayUndString的对象。在这个类中,只存在一个String [](用于播放乐器)和一个String(用于类型)。如果我只是将midi文件发送到服务器并发送响应200代码,该程序完全正常。但是,如果我想在从客户端接收midi文件后再分析它,然后以StringArrayUndString-object的形式发回分析的数据,我会收到以下错误:java.net.SocketException: Unexpected end of file from server
甚至响应代码都没有所示。该文件仍然正确传输。现在说话够了,这里是代码:
private static void sendPOST() throws IOException, ClassNotFoundException{
final int mid = 1;
final String POST_URL = "http://localhost:8080/musiker/hörprobe?mid="+mid;
final File uploadFile = new File("C://Users//Felix Ulbrich//Desktop//EIS Prototype MIDIs//Pop//IWantItThatWay.mid");
String boundary = Long.toHexString(System.currentTimeMillis());
String CRLF = "\r\n";
String charset = "UTF-8";
URLConnection connection = new URL(POST_URL).openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.setConnectTimeout(0);
connection.setReadTimeout(0);
try (
OutputStream output = connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
) {
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"binaryFile\"; filename=\"" + uploadFile.getName() + "\"").append(CRLF);
writer.append("Content-Length: " + uploadFile.length()).append(CRLF);
writer.append("Content-Type: " + URLConnection.guessContentTypeFromName(uploadFile.getName())).append(CRLF);
writer.append("Content-Transfer-Encoding: binary").append(CRLF);
writer.append(CRLF).flush();
Files.copy(uploadFile.toPath(), output);
output.flush();
int responseCode = ((HttpURLConnection) connection).getResponseCode();
if(responseCode == ((HttpURLConnection)connection).HTTP_OK){
InputStream input = connection.getInputStream();
ObjectInputStream ois = new ObjectInputStream(input);
Object object = ois.readObject();
if(object instanceof StringArrayUndString){
StringArrayUndString Daten = (StringArrayUndString)object;
for(int i = 0; i<Daten.array.length; i++){
System.out.println("Instrument: "+Daten.array[i]);
}
System.out.println("Genre: "+Daten.string);
}
}
System.out.println("Response code: ["+ responseCode + "]");
}
}
这是客户端。
System.out.println("Ich bin am Anfang!");
int hörprobenzaehler = 0;
Statement hoerprobenzaehlen = myConn.createStatement();
String hoerprobensql = "select MAX(hpid) from hörprobendaten where musiker_mid='"+musikerid+"'";
ResultSet anzahl = hoerprobenzaehlen.executeQuery(hoerprobensql);
while(anzahl.next()){
hörprobenzaehler = anzahl.getInt("max(hpid)");
}
String CRLF = "\r\n";
int fileSize = 0;
System.out.println("Ich erzeuge das File!");
String FILE_TO_RECEIVED = "C://root//m"+musikerid+"hp"+(hörprobenzaehler+1)+".mid";
File f = new File(FILE_TO_RECEIVED);
if (!f.exists()) {
f.createNewFile();
}
System.out.println("File erzeugt!");
InputStream input = t.getRequestBody();
String nextLine = "";
do {
nextLine = readLine(input, CRLF);
if (nextLine.startsWith("Content-Length:")) {
fileSize =
Integer.parseInt(
nextLine.replaceAll(" ", "").substring(
"Content-Length:".length()
)
);
}
System.out.println(nextLine);
} while (!nextLine.equals(""));
byte[] midFileByteArray = new byte[fileSize];
int readOffset = 0;
while(readOffset < fileSize){
int bytesRead = input.read(midFileByteArray, readOffset, fileSize);
readOffset += bytesRead;
}
System.out.println("Array gelesen!");
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(FILE_TO_RECEIVED));
bos.write(midFileByteArray, 0, fileSize);
System.out.println("Datei geschrieben!");
InstrumentIdentifier II = new InstrumentIdentifier(FILE_TO_RECEIVED);
DynamicIdentifier DI = new DynamicIdentifier(FILE_TO_RECEIVED);
MusicalTextureIdentifier MTI = new MusicalTextureIdentifier(FILE_TO_RECEIVED);
MidiAnalyzer analyzer = new MidiAnalyzer(II, DI, MTI);
StringArrayUndString Daten =analyzer.analyze();
System.out.println("File analysiert!");
bos.flush();
bos.close();
OutputStream output = t.getResponseBody();
System.out.println("Response Header ist raus!");
ObjectOutputStream oos = new ObjectOutputStream(output);
t.sendResponseHeaders(200, 0);
oos.writeObject(Daten);
System.out.println("Response Object ist raus!");
oos.flush();
oos.close();
System.out.println("Fertig!");
这是服务器端。
在服务器端,我使用了一些System.out.println命令,所以我会看到我的程序停止工作的位置,看起来,它会在System.out.println("Datei geschrieben!");
之后停止。如果有人知道解决方案,请告诉我:)。非常感谢你。
问候,Gizpo