1

My code:

public static  String newName =""; //the traditional Chinese file name
public static  String uploadFile ="";  //the file path contain traditional Chinese
public static  String ActionUrl ="";  //the server

public static void upload() {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
try {
URL url = new URL(ActionUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Accept", "text/*");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"folder\"" + end + end + "/mnt/HD/HD_a2/test/" + end);
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"Filedata\"; filename=\"" + newName + "\"" + end);
ds.writeBytes(end);
FileInputStream fStream = new FileInputStream(uploadFile);
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = -1;
while((length = fStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}       
ds.writeBytes(end);
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
fStream.close();
ds.flush();
InputStream is = con.getInputStream();
int ch;
StringBuffer b = new StringBuffer();
while((ch = is.read()) != -1) {
b.append((char)ch);
}
System.out.println("UPLOAD" + "SUCCESS");
ds.close();
}
catch(Exception e) {
e.printStackTrace();
}
}

It upload file success, but it show the garbled file name. How to modify it?

brian
  • 6,802
  • 29
  • 83
  • 124
  • Either the filename is not being provided in the expected character set (likely UTF-8), or the receiving server doesn't know to treat the incoming data in that character set. You might want to try using writeUTF instead of writeBytes when you're sending the filename, but that may be a problem in itself, since it doesn't write true UTF-8 that way, but rather [modified UTF-8](http://docs.oracle.com/javase/6/docs/api/java/io/DataInput.html#modified-utf-8) - and in cases where modified UTF-8 differs from true UTF-8, the server might complain. – Michael Madsen Jan 18 '12 at 01:32
  • Why are you using DataOutputStream? Why do you think it does the right thing with Chinese text via writeBytes? – bmargulies Jan 18 '12 at 01:33
  • I try to replace all writeBytes with writeUTF. But it does not show error message, but did not upload success. – brian Jan 18 '12 at 01:50
  • @brian: Then the issue might be with the server (perhaps it doesn't expect UTF-8), or maybe there's something else in the code. We can't easily tell unless you give more details (preferably the exact HTTP request that goes out; use a network or HTTP sniffer for that). Ideally, you should also check what happens if you try using a browser; if it doesn't work, then it's likely the server; if it does work that way, then compare the HTTP requests to see where they differ - that will tell you *where* the problem is, and then you can try to fix that specifically. – Michael Madsen Jan 18 '12 at 02:06
  • I try use browser to upload file which with chinese file name. It success. And I use wireshark to monitor the packet. The part of file name is the same. But my java client does not work, and browser client works. – brian Jan 18 '12 at 02:11
  • @brian: Check the *entire* request header, all the way down to the individual bytes if you have to. If the browser works, and your client doesn't, then the client is necessarily doing *something* different, and that difference causes the server to respond differently. We can't magically guess what that difference is. – Michael Madsen Jan 18 '12 at 02:39
  • I try to use wrtieUTF, it does not upload success. Even the file name does not exist chinese. But it id not show any error message. – brian Jan 18 '12 at 03:54
  • I get the difference. The writeUTF method will write the number of bytes at first. It make the server recognize fail. Have any method to avoid the difference? – brian Jan 18 '12 at 09:16

1 Answers1

2

try replace with below:

ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"folder\"" + end + end + "/mnt/HD/HD_a2/test/" + end);
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data;" + "name=\"Filedata\"; filename=\"");
ds.write(newName.getBytes("UTF-8"));
ds.writeBytes("\"" + end);
ds.writeBytes(end);