【发布时间】:2014-12-25 19:43:35
【问题描述】:
我在通过 FTP 上传文件到我的服务器时遇到问题。该文件是一个json文件,我读得很好,没有符号。这是我上传文件的代码:
public static void subir(){
String server = server;
int port = 21;
String user = user;
String pass = pass;
FTPClient ftpClient = new FTPClient();
ftpClient.setControlEncoding("UTF-8");
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("my.json");
String firstRemoteFile = "Folder/my.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("great");
}
} 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();
}
}
}
问题是,当我打开包含我的文件的 url 时,我用符号读取了我的信息,例如 parecÃa 而不是 parecía,Giménez 而不是 Giménez。有人可以帮助我吗?谢谢
我手动创建我的 json:
sBuffer = new StringBuffer();
a= "{\"results\":[";
sBuffer.append(a);
for (int z=0; z< myarray.size(); z++){
if (z<(myarray.size()-1)){
b="{\"one\":\""+ myarray.get(z).getOne()+"\",\"twoo\":\""+ myarray.get(z). getTwoo()+"\",\"three\":\""+ myarray.get(z).getThree()+"\"},";
sBuffer.append(b);
}
else{
c="{\"one\":\""+ myarray.get(z). getOne()+"\",\"twoo\":\""+ myarray.get(z).getTwoo()+"\",\"three\":\""+myarray.get(z). getThree()+"\"}]}";
sBuffer.append(c);
}
}
【问题讨论】:
-
您使用的 URL 应该发送一个
Content-type标头,指示 UTF-8。 -
我不明白,举个例子?谢谢
-
在我的服务器中的其他 json 中,我没有任何内容类型,问题是通过 ftp 上传
-
你说你访问了一个 URL。这可能意味着来自网络服务器的
http:...URL。现在,如果它没有在其Content-type标头中发送正确的字符编码,您将得到一个乱码文件(假设您的文件确实是UTF-8)。因此,您必须更改您在服务器端使用的任何内容(一个 servlet?一个 PHP 脚本?什么程序返回您的文件?),以便它在该标头中设置正确的字符编码。 -
问题可能不是通过 FTP 上传,而是其他 JSON 可能使用
\uXXXX符号对字符进行编码,因此它们没有编码问题。