我正在使用一項功能通過FTP將一個文件上傳到我的服務器。這裏是我的代碼,工作正常,但創建的文件example.json不是UTF8兼容的,因爲它具有Atlético而不是Atlético。有人可以告訴我這是多麼正確?由於UTF8兼容性
public static void subir(){
String server = myserver;
int port = 21;
String user = mouser;
String pass = mypass;
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, port);
ftpClient.login(user, pass);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// Uploads first file using an InputStream
File firstLocalFile = new File("example.json");
String firstRemoteFile = "MyDir/example.json";
InputStream inputStream = new FileInputStream(firstLocalFile);
System.out.println("Subiendo archivo a servidor...");
boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
inputStream.close();
if (done) {
System.out.println("Subido perfectamente");
}
} catch (IOException ex) {
System.out.println("Error: " + ex.getMessage());
ex.printStackTrace();
} finally {
try {
if (ftpClient.isConnected()) {
ftpClient.logout();
ftpClient.disconnect();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
,並保存我的文件,我用
public static void guardar(){
FileOutputStream fop = null;
File file;
String content = sBuffer.toString();
try {
file = new File("example.json");
fop = new FileOutputStream(file);
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
else{
file.createNewFile();
}
// get the content in bytes
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
System.out.println("Archivo guardado");
subir();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
你如何寫文件內容?你正在使用哪個類在你的文件中寫入數據? – varun
然後你如何閱讀*文件以查看Atlético? –
我已更新我的問題與創建和保存文件的功能 – Elena