acシャープベースのウェブサーバーにファイルをアップロードできるAndroidアプリを書きたいです。今のところ運がありません。プロトコルでサポートされていないアドレスファミリを取得し続けました。マニフェストファイルにINTERNETとWRITE_EXTERNAL_STORAGEを追加しました。これはエミュレーターで実行することに注意してください。
どんなアイデアや提案も本当にありがたいです。
よろしくお願いしますジャスティンザ
とにかくここに私のコードAndroidがあります-Uploader.java
package com.main;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;
public class Uploader extends AsyncTask<Object, String, Object>
{
URL connectURL;
String params;
String responseString;
String fileName;
byte[] dataToServer;
FileInputStream fileInputStream;
TextView info;
private static final String TAG = "Uploader";
void setUrlAndFile(String urlString, String fileName, TextView info)
{
Log.d(TAG,"StartUploader");
this.info = info;
try
{
fileInputStream = new FileInputStream(fileName);
connectURL = new URL(urlString);
}
catch(Exception e)
{
publishProgress(e.toString());
}
this.fileName = fileName;
}
synchronized void doUpload()
{
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
Log.d(TAG,"lv1");
try
{
Log.d(TAG,"doUpload");
publishProgress("Uploading...");
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection","Keep-Alive");
conn.setRequestProperty("Content-Type","multipart/form-data;boundary=" + boundary);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition:form-data; name=\"uploadedfile\";filename=\"" + fileName + "\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.d(TAG,"LvA");
Log.d(TAG,twoHyphens + boundary + lineEnd + ";Content-Disposition:form-data; name=\"uploadedfile\";filename=\"" + fileName + "\"" + lineEnd);
int bytesAvailable = fileInputStream.available();
int maxBufferSize = 1024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[] buffer = new byte[bufferSize];
int bytesRead = fileInputStream.read(buffer,0, bufferSize);
Log.d(TAG,"LvB");
while(bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}
dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
fileInputStream.close();
dos.flush();
InputStream is = conn.getInputStream();
int ch;
Log.d(TAG,"LvC");
StringBuffer buff = new StringBuffer();
while((ch=is.read()) != -1)
{
buff.append((char)ch);
}
publishProgress(buff.toString());
dos.close();
Log.d(TAG,"lv2");
}
catch(Exception e)
{
publishProgress(e.toString());
}
}
@Override
protected Object doInBackground(Object... arg0)
{
Log.d(TAG,"lv1a");
doUpload();
Log.d(TAG,"lv1b");
return null;
}
protected void onProgressUpdate(String... progress)
{
this.info.setText(progress[0]);
}
}
これが主な活動です
package com.main;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilePermission;
import java.io.OutputStreamWriter;
import java.net.SocketException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
public class ReadWriteToPCActivity extends Activity
{
private static final String TAG = "ReadWriteToPCActivity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View upload = findViewById(R.id.Upload);
final EditText path = (EditText)findViewById(R.id.Path);
final TextView info = (TextView)findViewById(R.id.Info);
Log.d(TAG,"Start");
File fn = new File(Environment.getExternalStorageDirectory()+"/new.xml");
upload.setOnClickListener
(
new OnClickListener()
{
public void onClick(View v)
{
try
{
Log.d(TAG,"Step1");
WriteFile();
Uploader uploader = new Uploader();
//uploader.setUrlAndFile("http://192.168.0.123/receiver.aspx", path.getText().toString(), info);
uploader.setUrlAndFile("http://192.168.0.123/personalfinance/receiver.aspx", Environment.getExternalStorageDirectory()+"/samplefile.txt", info);
//uploader.setUrlAndFile("http://10.0.2.2/personalfinance/receiver.aspx", Environment.getExternalStorageDirectory()+"/new.xml", info);
//uploader.setUrlAndFile("http://192.168.0.123/personalfinance/ReceiveUpload.aspx", Environment.getExternalStorageDirectory()+"/new.xml", info);
Log.d(TAG,"Upload." + Environment.getExternalStorageDirectory()+"/samplefile.txt;" + info.getText().toString());
uploader.execute();
}
catch(Exception e)
{
path.setText(e.toString());
Log.d(TAG,e.toString());
}
}
private void WriteFile()
{
try
{
Log.d(TAG,"Step 1 write file");
File newfile = new File(Environment.getExternalStorageDirectory() + "/samplefile.txt");
Log.d(TAG,"Step 2 write file");
if(!newfile.exists() && !newfile.createNewFile())
{
Log.d(TAG,"File exists");
}
Log.d(TAG,"cp3");
newfile.createNewFile();
Log.d(TAG,"Step 3 write file");
FileOutputStream fos = null;
Log.d(TAG,"Step 4 write file");
fos = new FileOutputStream(newfile);
Log.d(TAG,"Step 5 write file");
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("Hello Android");
osw.flush();
osw.close();
FilePermission fp;
fp = new FilePermission(Environment.getExternalStorageDirectory()+"/samplefile.txt","read");
Log.d(TAG,"File created");
}
catch(Exception e)
{
Log.d(TAG,e.toString());
}
}
}
);
}
}
そして最後に、これは私のレシーバーファイルです
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PersonalFinance
{
public partial class receiver : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
HttpFileCollection uploadFiles = Request.Files;
HttpPostedFile postedFile = uploadFiles[0];
System.IO.Stream inStream = postedFile.InputStream;
byte[] fileData = new byte[postedFile.ContentLength];
inStream.Read(fileData, 0, postedFile.ContentLength);
postedFile.SaveAs(Server.MapPath("Data") + "\\" + postedFile.FileName);
}
}
}