zipファイルをダウンロードして終了するだけの非常に必要最低限のファイルサーバーを作成しようとしていますが、問題が発生しています。私には2つの主な問題があります。
1つ目は、ローカルホストでテストすると、zipファイルを転送するという点で機能しますが、開こうとすると、破損しているというエラーが表示されます。それはzipファイル形式または私がそれを転送する方法と関係があるかもしれません。
2番目の問題は、ローカルホスト以外を使用すると失敗することです。Webサイトを自分のIPにリダイレクトしようとしましたが、IPアドレスを入力するだけで、すべてのファイアウォールとウイルス対策をオフにしても、両方とも失敗します。
サーバーコード:
import java.io.*;
import java.net.*;
public class FileServer {
public static void main(String[] args) throws IOException {
final int PORT_NUMBER = 44444;
ServerSocket serverSock = null;
PrintWriter out = null;
BufferedInputStream bin = null;
OutputStream os = null;
Socket clientSock = null;
File file;
byte[] fileData;
String filename = "file.zip";
while(true) {
try {
//Listen on port
serverSock = new ServerSocket(PORT_NUMBER);
//Get connection
clientSock = serverSock.accept();
System.out.println("Connected client");
//Get output stream
out = new PrintWriter(clientSock.getOutputStream(), true);
out.println(filename); //Print filename
file = new File(filename); //Get file
fileData = new byte[(int)file.length()]; //Stores the file data
bin = new BufferedInputStream(new FileInputStream(file));
out.println((int)file.length()); //Print filesize
bin.read(fileData); //Read contents of file
os = clientSock.getOutputStream();
os.write(fileData); //Write the file data
os.flush();
} catch(SocketException e) {
System.out.println("Client disconnected");
} catch(Exception e) {
System.out.println(e.getMessage());
System.exit(1);
} finally {
//Close all connections
System.out.println("Shutting down");
if(os != null) {
os.close();
}
if(bin != null) {
bin.close();
}
if(out != null) {
out.close();
}
if(clientSock != null) {
clientSock.close();
}
if(serverSock != null) {
serverSock.close();
}
}
}
}
}
クライアントコードスニペット。スニペットを切り取ったときに中かっこなどが一致しなかった可能性があるため、すべての構文が正しく、他のすべてが存在して機能していると想定します。
import java.io.*;
import java.net.*;
import javax.swing.JOptionPane;
public static void main(String[] args) {
final int PORT_NUMBER = 44444;
final String HOSTNAME = "127.0.0.1";
String filename = "default.txt";
Socket sock = null;
BufferedReader in = null;
BufferedOutputStream bos = null;
InputStream is = null;
byte[] fileData;
//Attempt to connect
try {
sock = new Socket(HOSTNAME, PORT_NUMBER);
in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
is = sock.getInputStream();
} catch(UnknownHostException e) {
JOptionPane.showMessageDialog(this, "Error: could not connect to host " + HOSTNAME + " on port number " + PORT_NUMBER);
System.exit(1);
} catch(ConnectException e) {
JOptionPane.showMessageDialog(this, "Error: connection refused");
System.exit(1);
} catch(Exception e) {
JOptionPane.showMessageDialog(this, e);
System.exit(1);
}
try {
filename = in.readLine();
bos = new BufferedOutputStream(new FileOutputStream(filename));
fileData = new byte[Integer.decode(in.readLine())]; //Gets file size
is.read(fileData);
bos.write(fileData);
bos.flush();
bos.close();
if(is != null) {
is.close();
}
if(in != null) {
in.close();
}
if(bos != null) {
bos.close();
}
if(sock != null) {
sock.close();
}
} catch(Exception e) {
JOptionPane.showMessageDialog(this, e);
System.exit(1);
}
JOptionPane.showMessageDialog(this, "Download complete");
}
}
}
}
編集:.docおよび.docxファイルを使用するローカルホストで問題なく動作します。問題を引き起こすのは.zipのみです。