FileInputStream 运行 是提示 No such file or directory ,这个怎么解决。
uri.getPath() 不是获取的文件的地址吗?为什么上传还会报错呢?下面是源代码package com.example.dataup;import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;import java.io.File;public class MainActivity extends Activity { private Button btn1;
private Button btn2; private String uploadUrl = "http://www.maizitime.com:8081/upload_test";
private String filePath;
private String filePath1;
private String filePath2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("test", "程序启动");
Log.e("test", "程序启动");
Log.d("test", "程序启动");
Log.v("test", "程序启动");
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2); btn1.setOnClickListener(new ButtonClickListener());
btn2.setOnClickListener(new ButtonClickListener());
} class ButtonClickListener implements View.OnClickListener { @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1: chooseFile(); break; case R.id.btn2: new Thread(new Runnable() {
@Override
public void run() {
UploadUtil.uploadFile(new File(filePath), uploadUrl);
}
}).start(); break; default:
break;
}
}
} private void chooseFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 1);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
filePath = uri.getPath();
Log.i("test", "filePath1:" + filePath);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
package com.example.dataup;import android.util.Log;import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;/**
* Created by blue on 2016/3/26.
*/
public class UploadUtil { private static final int TIME_OUT = 10 * 1000;
private static final String CHARSET = "UTF-8"; public static int uploadFile(File file, String requestURL) {
int res = 0; String BOUNDARY = UUID.randomUUID().toString();// boundary
String PREFIX = "--";
String LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; try {
URL url = new URL(requestURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(TIME_OUT);
httpURLConnection.setConnectTimeout(TIME_OUT);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Charset", CHARSET);
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Content-type", CONTENT_TYPE + ";boundary=" + BOUNDARY); if (file != null) {
Log.i("test", "upload start1");
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END);
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"" + LINE_END);
sb.append("Content-Type: application/octet-stream; Charset=" + CHARSET + LINE_END);
sb.append(LINE_END); Log.i("test", "upload start2");
Log.i("test", "upload start3");
dataOutputStream.write(sb.toString().getBytes());
Log.i("test", "upload start4"+file);
InputStream inputStream = new FileInputStream(file);
Log.i("test", "upload start5"+inputStream);
byte[] bytes = new byte[1024];
int len = 0;
Log.i("test", "upload start5");
while ((len = inputStream.read(bytes)) != -1) {
dataOutputStream.write(bytes, 0, len);
Log.i("test", "upload bytes:" + len);
}
Log.i("test", "upload start6");
inputStream.close();
dataOutputStream.write(LINE_END.getBytes()); String end = PREFIX + BOUNDARY + PREFIX + LINE_END;
dataOutputStream.write(end.getBytes());
dataOutputStream.flush();
Log.i("test", "upload end"); res = httpURLConnection.getResponseCode();
if (res == 200) {
Log.i("test", "upload success");
InputStream inputStream1 = httpURLConnection.getInputStream();
StringBuffer stringBuffer = new StringBuffer();
while ((len = inputStream1.read(bytes)) != -1) {
stringBuffer.append(new String(bytes, 0, len, "UTF-8"));
}
Log.i("test", "result:" + stringBuffer.toString());
} else {
Log.i("test", "upload fail");
}
} } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return res;
}
}
uri.getPath() 不是获取的文件的地址吗?为什么上传还会报错呢?下面是源代码package com.example.dataup;import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;import java.io.File;public class MainActivity extends Activity { private Button btn1;
private Button btn2; private String uploadUrl = "http://www.maizitime.com:8081/upload_test";
private String filePath;
private String filePath1;
private String filePath2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i("test", "程序启动");
Log.e("test", "程序启动");
Log.d("test", "程序启动");
Log.v("test", "程序启动");
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2); btn1.setOnClickListener(new ButtonClickListener());
btn2.setOnClickListener(new ButtonClickListener());
} class ButtonClickListener implements View.OnClickListener { @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1: chooseFile(); break; case R.id.btn2: new Thread(new Runnable() {
@Override
public void run() {
UploadUtil.uploadFile(new File(filePath), uploadUrl);
}
}).start(); break; default:
break;
}
}
} private void chooseFile() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, 1);
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
filePath = uri.getPath();
Log.i("test", "filePath1:" + filePath);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
package com.example.dataup;import android.util.Log;import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;/**
* Created by blue on 2016/3/26.
*/
public class UploadUtil { private static final int TIME_OUT = 10 * 1000;
private static final String CHARSET = "UTF-8"; public static int uploadFile(File file, String requestURL) {
int res = 0; String BOUNDARY = UUID.randomUUID().toString();// boundary
String PREFIX = "--";
String LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; try {
URL url = new URL(requestURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setReadTimeout(TIME_OUT);
httpURLConnection.setConnectTimeout(TIME_OUT);
httpURLConnection.setDoInput(true);
httpURLConnection.setDoOutput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Charset", CHARSET);
httpURLConnection.setRequestProperty("Connection", "Keep-Alive");
httpURLConnection.setRequestProperty("Content-type", CONTENT_TYPE + ";boundary=" + BOUNDARY); if (file != null) {
Log.i("test", "upload start1");
DataOutputStream dataOutputStream = new DataOutputStream(httpURLConnection.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END);
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"" + LINE_END);
sb.append("Content-Type: application/octet-stream; Charset=" + CHARSET + LINE_END);
sb.append(LINE_END); Log.i("test", "upload start2");
Log.i("test", "upload start3");
dataOutputStream.write(sb.toString().getBytes());
Log.i("test", "upload start4"+file);
InputStream inputStream = new FileInputStream(file);
Log.i("test", "upload start5"+inputStream);
byte[] bytes = new byte[1024];
int len = 0;
Log.i("test", "upload start5");
while ((len = inputStream.read(bytes)) != -1) {
dataOutputStream.write(bytes, 0, len);
Log.i("test", "upload bytes:" + len);
}
Log.i("test", "upload start6");
inputStream.close();
dataOutputStream.write(LINE_END.getBytes()); String end = PREFIX + BOUNDARY + PREFIX + LINE_END;
dataOutputStream.write(end.getBytes());
dataOutputStream.flush();
Log.i("test", "upload end"); res = httpURLConnection.getResponseCode();
if (res == 200) {
Log.i("test", "upload success");
InputStream inputStream1 = httpURLConnection.getInputStream();
StringBuffer stringBuffer = new StringBuffer();
while ((len = inputStream1.read(bytes)) != -1) {
stringBuffer.append(new String(bytes, 0, len, "UTF-8"));
}
Log.i("test", "result:" + stringBuffer.toString());
} else {
Log.i("test", "upload fail");
}
} } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return res;
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货