Java Exeception get inputStream from connection 404 responce和file not found异常?
我想尝试从android studio中的简历下载一个文件,我已经用OpenStream尝试了我的代码,没有任何问题,但在以下代码中:Java Exeception get inputStream from connection 404 responce和file not found异常?,java,android,Java,Android,我想尝试从android studio中的简历下载一个文件,我已经用OpenStream尝试了我的代码,没有任何问题,但在以下代码中: public void startStream2(Context context) { try { URL url = new URL(file.getFileUrl()); StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().per
public void startStream2(Context context) {
try {
URL url = new URL(file.getFileUrl());
StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ploicy);
URLConnection connection = url.openConnection();
int downloaded = 0;
BufferedOutputStream bout;
f = new File(downloadPath, file.getName());
if (f.exists()) {
downloaded = (int) f.length();
connection.setRequestProperty("Range", "bytes=" + (f.length()) + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
connection.setRequestProperty("Accept","*/*");
connection.connect();
int response=((HttpURLConnection)connection).getResponseCode();
Log.e(TAG, "startStream2: "+ response );
if (response>399 && response<601){
InputStreamReader sr;
sr = new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8");
StringBuilder builder = new StringBuilder();
for (int bt = 0; (bt = sr.read()) != -1;) {
builder.append((char)bt);
}
sr.close();
Log.e(TAG, "startStream2: "+builder.toString());
}
InputStream inp=connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(inp);
stream2 = (downloaded == 0) ? new FileOutputStream(f) : new FileOutputStream(f, true);
bout = new BufferedOutputStream(stream2, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
int percent = ((int) downloaded * 100) / (int) Size;
//set percent progress
}
}catch (Exception e){
Log.e(TAG, "startStream2: ",e );
}
}
public void startStream2(Context context) {
try {
URL url = new URL(file.getFileUrl());
StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ploicy);
URLConnection connection = url.openConnection();
int downloaded = 0;
BufferedOutputStream bout;
f = new File(downloadPath, file.getName());
if (f.exists()) {
downloaded = (int) f.length();
connection.setRequestProperty("Range", "bytes=" + (f.length()) + "-");
}
connection.setDoInput(true);
connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
connection.setRequestProperty("Accept","*/*");
int response=((HttpURLConnection)connection).getResponseCode();
Log.e(TAG, "startStream2: "+ response );
if (response>399 && response<601){
InputStreamReader sr;
sr = new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8");
StringBuilder builder = new StringBuilder();
for (int bt = 0; (bt = sr.read()) != -1;) {
builder.append((char)bt);
}
sr.close();
Log.e(TAG, "startStream2: "+builder.toString());
}
InputStream inp=connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(inp);
stream2 = (downloaded == 0) ? new FileOutputStream(f) : new FileOutputStream(f, true);
bout = new BufferedOutputStream(stream2, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
int percent = ((int) downloaded * 100) / (int) Size;
//set percent progress
}
}catch (Exception e){
Log.e(TAG, "startStream2: ",e );
}
}
public void startStream2(上下文){
试一试{
URL URL=新URL(file.getFileUrl());
StrictMode.ThreadPolicy ploicy=新StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ploice);
URLConnection=url.openConnection();
int=0;
缓冲输出流;
f=新文件(下载路径,File.getName());
如果(f.exists()){
下载=(int)f.length();
setRequestProperty(“范围”、“字节数=“+(f.length())+”-”;
}
connection.setDoInput(true);
connection.setDoOutput(真);
setRequestProperty(“用户代理”,“Mozilla/5.0(兼容)”);
connection.setRequestProperty(“接受”,“*/*”);
connection.connect();
int response=((HttpURLConnection)connection.getResponseCode();
Log.e(标签“startStream2:+响应”);
如果(响应>399&&response=0){
写(数据,0,x);
下载+=x;
整数百分比=((整数)下载*100)/(整数)大小;
//设置进度百分比
}
}捕获(例外e){
Log.e(标签“startStream2:”,e);
}
}
错误日志:
startStream2:405 startStream2:
405不允许
405不允许
nginx startStream2: java.io.FileNotFoundException: 在 com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242) 我以前测试过url,从first或resume下载时没有任何问题。 我如何解决它? 谢谢。删除这些行
connection.setDoInput(true);
connection.setDoOutput(true);
问题就在这里。它将HTTP谓词从GET更改为POST。你不想发帖。您没有发送任何输出。移除它
如果响应代码不是200,则不应获取输入流,否则它将引发异常。如果需要更多信息,可以获取错误流,但此时没有输入流
您还可以删除
connection.connect();
及
他们不会做任何尚未发生的事情。最后,我用以下代码解决了这个问题:
public void startStream2(Context context) {
try {
URL url = new URL(file.getFileUrl());
StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ploicy);
URLConnection connection = url.openConnection();
int downloaded = 0;
BufferedOutputStream bout;
f = new File(downloadPath, file.getName());
if (f.exists()) {
downloaded = (int) f.length();
connection.setRequestProperty("Range", "bytes=" + (f.length()) + "-");
}
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
connection.setRequestProperty("Accept","*/*");
connection.connect();
int response=((HttpURLConnection)connection).getResponseCode();
Log.e(TAG, "startStream2: "+ response );
if (response>399 && response<601){
InputStreamReader sr;
sr = new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8");
StringBuilder builder = new StringBuilder();
for (int bt = 0; (bt = sr.read()) != -1;) {
builder.append((char)bt);
}
sr.close();
Log.e(TAG, "startStream2: "+builder.toString());
}
InputStream inp=connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(inp);
stream2 = (downloaded == 0) ? new FileOutputStream(f) : new FileOutputStream(f, true);
bout = new BufferedOutputStream(stream2, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
int percent = ((int) downloaded * 100) / (int) Size;
//set percent progress
}
}catch (Exception e){
Log.e(TAG, "startStream2: ",e );
}
}
public void startStream2(Context context) {
try {
URL url = new URL(file.getFileUrl());
StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ploicy);
URLConnection connection = url.openConnection();
int downloaded = 0;
BufferedOutputStream bout;
f = new File(downloadPath, file.getName());
if (f.exists()) {
downloaded = (int) f.length();
connection.setRequestProperty("Range", "bytes=" + (f.length()) + "-");
}
connection.setDoInput(true);
connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
connection.setRequestProperty("Accept","*/*");
int response=((HttpURLConnection)connection).getResponseCode();
Log.e(TAG, "startStream2: "+ response );
if (response>399 && response<601){
InputStreamReader sr;
sr = new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8");
StringBuilder builder = new StringBuilder();
for (int bt = 0; (bt = sr.read()) != -1;) {
builder.append((char)bt);
}
sr.close();
Log.e(TAG, "startStream2: "+builder.toString());
}
InputStream inp=connection.getInputStream();
BufferedInputStream in = new BufferedInputStream(inp);
stream2 = (downloaded == 0) ? new FileOutputStream(f) : new FileOutputStream(f, true);
bout = new BufferedOutputStream(stream2, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0) {
bout.write(data, 0, x);
downloaded += x;
int percent = ((int) downloaded * 100) / (int) Size;
//set percent progress
}
}catch (Exception e){
Log.e(TAG, "startStream2: ",e );
}
}
public void startStream2(上下文){
试一试{
URL URL=新URL(file.getFileUrl());
StrictMode.ThreadPolicy ploicy=新StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(ploice);
URLConnection=url.openConnection();
int=0;
缓冲输出流;
f=新文件(下载路径,File.getName());
如果(f.exists()){
下载=(int)f.length();
setRequestProperty(“范围”、“字节数=“+(f.length())+”-”;
}
connection.setDoInput(true);
setRequestProperty(“用户代理”,“Mozilla/5.0(兼容)”);
connection.setRequestProperty(“接受”,“*/*”);
int response=((HttpURLConnection)connection.getResponseCode();
Log.e(标签“startStream2:+响应”);
如果(响应>399&&response=0){
写(数据,0,x);
下载+=x;
整数百分比=((整数)下载*100)/(整数)大小;
//设置进度百分比
}
}捕获(例外e){
Log.e(标签“startStream2:”,e);
}
}
非常感谢 我发送属性它需要做输出不需要?不需要。如果要调用
getOutputStream()
并向其写入内容,您只需要setDoOutput(true)
。我想这意味着这个答案解决了这个问题。如果有不同的解决方案,你应该把它贴在这里为他人的利益。你应该考虑支持和/或接受你在这里找到有用的答案。这不提供任何解释,而只是重复其他已经回答的问题。