[追記]
サーバーのエラーメッセージは無しです。
{"status":500,"message":"file not uploaded."}
[/追記]
[追記]
できないというのは、ファイルがサーバーにアップロードされないと言う意味です。
サーバーにアップロードされないのはどうやって気がついたかと言うと
ステータスが500で帰ってきたからです。
[/追記]
サーバーをPHPで書いてそのサーバーにファイルをポストするJAVAプログラムを下記を参考(ほぼコピペして)に書いたの
http://androidnote.logdown.com/posts/152809-http-messenger
うまくファイルをPOSTできません。
東方プログラミング初心者でJavaはそもそも苦手で全く詳しくありません。
誰か助けていただけないでしょうか...?
ちなみにURLとファイル名はしっかり入力しました。
よろしくお願いいたします。
lang
1import java.io.BufferedReader; 2import java.io.DataOutputStream; 3import java.io.File; 4import java.io.FileInputStream; 5import java.io.FileNotFoundException; 6import java.io.InputStreamReader; 7import java.net.HttpURLConnection; 8import java.net.URL; 9 10public class Main { 11 12 public static void main(String[] args){ 13 sendData("URL","ファイル名"); 14 } 15 16 17 public static void sendData(String api, String filename) { 18 19 HttpURLConnection connection = null; 20 DataOutputStream outputStream = null; 21 22 String lineEnd = "\r\n"; 23 String twoHyphens = "--"; 24 String boundary = "*****"; 25 26 int bytesRead, bytesAvailable, bufferSize; 27 byte[] buffer; 28 int maxBufferSize = 1 * 1024 * 1024; 29 30 try { 31 // サーバへの接続開始 32 URL url = new URL(api); 33 connection = (HttpURLConnection) url.openConnection(); 34 35 connection.setDoInput(true); 36 connection.setDoOutput(true); 37 38 // キャッシュを使用しない 39 connection.setUseCaches(false); 40 41 connection.setRequestMethod("POST"); 42 // HTTP/1.1 でサポートされた持続接続機能が使用できることを宣言する 43 connection.setRequestProperty("Connection", "Keep-Alive"); 44 45 // マルチパートデータとして複数のデータを送ることを宣言する 46 connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); 47 48 StringBuilder builder = new StringBuilder(); 49 builder.append(twoHyphens + boundary + lineEnd); 50 builder.append("Content-Disposition: form-data; name=\"id\"" + lineEnd); 51 builder.append(lineEnd); 52 builder.append(lineEnd); 53 builder.append(twoHyphens + boundary + lineEnd); 54 builder.append("Content-Disposition: form-data; name=\"password\"" + lineEnd); 55 builder.append(lineEnd); 56 builder.append(lineEnd); 57 builder.append(twoHyphens + boundary + lineEnd); 58 builder.append("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + filename + "\"" + lineEnd); 59 builder.append(lineEnd); 60 File file = new File(filename); 61 FileInputStream fileInputStream = new FileInputStream(file); 62 StringBuilder lastbuilder = new StringBuilder(); 63 lastbuilder.append(lineEnd + twoHyphens + boundary + twoHyphens + lineEnd); 64 65 // 内部バッファ処理を行わずに HTTP 要求本体のストリーミングを有効にする 66 connection.setChunkedStreamingMode(0); 67 68 // setFixedLengthStreamingMode をつかう場合 69 // Content-length を算出する 70 // int contentLength = (int) (builder.toString().getBytes().length 71 // + lastbuilder.toString().getBytes().length 72 // + file.length()); 73 // connection.setFixedLengthStreamingMode(contentLength); 74 75 outputStream = new DataOutputStream(connection.getOutputStream()); 76 outputStream.writeBytes(builder.toString()); 77 78 // ファイルを一定サイズ毎読み書きする 79 bytesAvailable = fileInputStream.available(); 80 bufferSize = Math.min(bytesAvailable, maxBufferSize); 81 buffer = new byte[bufferSize]; 82 bytesRead = fileInputStream.read(buffer, 0, bufferSize); 83 while (bytesRead > 0) { 84 outputStream.write(buffer, 0, bufferSize); 85 bytesAvailable = fileInputStream.available(); 86 bufferSize = Math.min(bytesAvailable, maxBufferSize); 87 bytesRead = fileInputStream.read(buffer, 0, bufferSize); 88 } 89 outputStream.writeBytes(lastbuilder.toString()); 90 91 String result = ""; 92 93 // 成功ならレスポンスボディをそのまま表示する 94 if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { 95 BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); 96 String line = null; 97 while ((line = br.readLine()) != null) { 98 result += line + "\n"; 99 } 100 } else { 101 result = "Server error is occured"; 102 } 103 104 try { 105 fileInputStream.close(); 106 outputStream.flush(); 107 outputStream.close(); 108 connection.disconnect(); 109 } catch (Exception e) { 110 // Android 2.2 以下だと flush() あたりで java.io.IOException: Stream is 111 // closed が発生するが、ファイルは無事に届いているはず。 112 // Default buffer size used in BufferedReader constructor. It 113 // would be better to be explicit if an 8k-char buffer is 114 // required. が関係しているような気がする。 115 e.printStackTrace(); 116 } 117 118 119 120 System.out.println(result); 121 } catch (FileNotFoundException e) { 122 123 } catch (Exception ex) { 124 125 } 126 } 127} 128
lang
1<?php 2 3require_once "twitter.php"; 4 5$domain = empty($_SERVER["HTTPS"]) ? "http://" : "https://"; 6$domain .= $_SERVER["HTTP_HOST"]; 7$postName = "upload"; 8$response = array(); 9 10if (is_uploaded_file($_FILES[$postName]['tmp_name'])) { 11 if (move_uploaded_file($_FILES[$postName]['tmp_name'],"./upload/".$_FILES[$postName]['name'])) { 12 $response['status'] = 200; 13 $response['message'] = "uploaded"; 14 $response['twitter'] = tweetUpfile( $domain . "/upload/" . $_FILES[$postName]['name']); 15 } else { 16 $response['status'] = 500; 17 $response['message'] = "error while saving."; 18 } 19} else { 20 $response['status'] = 500; 21 $response['message'] = "file not uploaded."; 22} 23 24header('Content-Type: application/json'); 25echo json_encode($response); 26