###androidでファイル送信のファイル名文字化け
androidにてファイルの送信をhttpconnectionを用いて行っているのですが、
その際にArt��՚�.docxのように文字化けしてしまいます。
###ソースコードは
java
1HttpsURLConnection.setFollowRedirects(false); 2 HttpsURLConnection connection = null; 3 String fileName = file.getName(); 4 try { 5 connection = (HttpsURLConnection) new URL(url).openConnection(); 6 connection.setRequestMethod("POST"); 7 String boundary = "---------------------------boundary"; 8 String tail = "\r\n--" + boundary + "--\r\n"; 9 connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); 10 connection.setDoOutput(true); 11 12 String metadataPart = "--" + boundary + "\r\n" 13 + "Content-Disposition: form-data; name=\"metadata\"\r\n\r\n" 14 + "" + "\r\n"; 15 16 String fileHeader1 = "--" + boundary + "\r\n" 17 + "Content-Disposition: form-data; name=\""+ Const.Params.FILE +"\"; filename=\"" 18 + fileName + "\"\r\n" 19 + "Content-Type: " + contentType + "\r\n" 20 + "Content-Transfer-Encoding: binary\r\n"; 21 22 long fileLength = file.length() + tail.length(); 23 String fileHeader2 = "Content-length: " + fileLength + "\r\n"; 24 String fileHeader = fileHeader1 + fileHeader2 + "\r\n"; 25 String stringData = metadataPart + fileHeader; 26 27 listener.onSetMax((int) fileLength); 28 29 long requestLength = stringData.length() + fileLength; 30 connection.setRequestProperty("Content-length", "" + requestLength); 31 connection.setFixedLengthStreamingMode((int) requestLength); 32 connection.connect(); 33 34 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); 35 out.writeBytes(stringData); 36 37 int progress = 0; 38 int bytesRead = 0; 39 byte buf[] = new byte[1024]; 40 BufferedInputStream bufInput = new BufferedInputStream(new FileInputStream(file)); 41 while ((bytesRead = bufInput.read(buf)) != -1) { 42 // write output 43 out.write(buf, 0, bytesRead); 44 out.flush(); 45 progress += bytesRead; 46 // update progress bar 47 listener.onProgress(progress); 48 } 49 50 listener.onFinishUpload(); 51 52 // Write closing boundary and close stream 53 out.writeBytes(tail); 54 out.flush(); 55 out.close(); 56 57 // Get server response 58 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 59 String line = ""; 60 StringBuilder builder = new StringBuilder(); 61 while((line = reader.readLine()) != null) { 62 builder.append(line); 63 } 64 65 response = builder.toString(); 66 67 } catch (UnknownHostException e) { 68 e.printStackTrace(); 69 response = String.valueOf(Const.UploadFileErrorTypes.NO_HOST_TYPE); 70 } catch (Exception e) { 71 e.printStackTrace(); 72 response = String.valueOf(Const.UploadFileErrorTypes.CONNECTION_ERROR_TYPE); 73 } finally { 74 if (connection != null) connection.disconnect(); 75 }
上記のfileNameが文字化けしています。
###試したこと
fileNameをutf-8にするなどは試したのですが、こちらがiphone,webなどに送信するため、decode処理を行えず%のまま表示されてしまいます。
送信前に日本語として表示することはできないでしょうか。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/04/12 09:24
2017/04/12 09:48