私はファイルをサーバーにアップロードしようとしています。そのために非常に多くの異なるコードを試しましたが、成功しません。
このコードでは、接続は正常に作成されますが、ヘッダーを作成してファイルを読み取り、サーバーにデータを投稿するときに...
class ConnectionThread extends Thread
{
DataOutputStream outputStream = null;
DataInputStream inputStream = null;
String boundary = "*****";
String lineEnd = "\r\n";
String twoHyphens = "--";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 2*1024*1024;
DataInputStream fileInputStream = null;
public void run()
{
try {
ConnectionFactory connFact = new ConnectionFactory();
ConnectionDescriptor connDesc;
connDesc = connFact.getConnection("http://www.myserver/upload.php");
if (connDesc != null)
{
HttpConnection conn;
conn = (HttpConnection)connDesc.getConnection();
conn.setRequestMethod(conn.POST);
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("ENCTYPE", "multipart/form-data");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
outputStream = new DataOutputStream( conn.openDataOutputStream() );
outputStream.writeChars(twoHyphens + boundary + lineEnd);
outputStream.writeChars("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + "files:///SDCard/bb.txt" +"\"" + lineEnd);
outputStream.writeChars(lineEnd);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
FileConnection fis=(FileConnection)Connector.open("file:///SDCard/bb.txt");
InputStream inputStream = fis.openInputStream();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int buffersize=1024*1024;
byte[] buffer=new byte[buffersize];
int length=0;
while((length=inputStream.read(buffer))!=-1)
{
bos.write(buffer,0,length);
}
byte[] imagedata=bos.toByteArray();
outputStream.write(imagedata);
outputStream.writeChars(lineEnd);
outputStream.writeChars(twoHyphens + boundary + twoHyphens + lineEnd);
// Responses from the server (code and message)
int serverResponseCode = conn.getResponseCode();
final String serverResponseMessage = conn.getResponseMessage();
fileInputStream.close();
outputStream.flush();
outputStream.close();
}
}
catch (Exception e) {
}
}
plz誰かが助けて...事前に感謝します。