我收到FileNotFoundException
错误。
代码:
File getpath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); String dir = getpath.getAbsolutePath(); Log.e(“dir的文件名”,dir);
//"/storage/emulated/0/Movies/"
try {
FileInputStream fstrm = new FileInputStream(dir+filename);
VideoFileUploadNew hfu = new VideoFileUploadNew( ServerURL.VIDEO_UPLOAD, filename);
upflag = hfu.Send_Now(fstrm);
Log.e("filename of v up",filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
公共类VideoFileUploadNew实现了Runnable {
URL connectURL;
String responseString;
String Title;
String fileName;
String Description;
byte[ ] dataToServer;
FileInputStream fileInputStream = null;
public VideoFileUploadNew(String urlString, String file){
try{
connectURL = new URL(urlString);
fileName = file;
}catch(Exception ex){
Log.i("HttpFileUpload","URL Malformatted");
}
}
public Boolean Send_Now(FileInputStream fStream){
fileInputStream = fStream;
return Sending();
}
Boolean Sending(){
System.out.println("file Name is :"+fileName);
String iFileName = fileName;
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = "*****";
String Tag="fSnd";
try
{
Log.e(Tag,"Starting Http File Sending to URL");
// Open a HTTP connection to the URL
HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection();
// Allow Inputs
conn.setDoInput(true);
// Allow Outputs
conn.setDoOutput(true);
// Don't use a cached copy.
conn.setUseCaches(false);
// Use a post method.
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("Content-Disposition: form-data; name=\"vfile\";filename=\"" + iFileName +"\"" + lineEnd);
dos.writeBytes(lineEnd);
Log.e(Tag,"Headers are written");
// create a buffer of maximum size
int bytesAvailable = fileInputStream.available();
int maxBufferSize =9024;
int bufferSize = Math.min(bytesAvailable, maxBufferSize);
byte[ ] buffer = new byte[bufferSize];
// read file and write it into form...
int bytesRead = fileInputStream.read(buffer, 0, bufferSize);
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);
// close streams
fileInputStream.close();
dos.flush();
Log.e(Tag,"File Sent, Response: "+String.valueOf(conn.getResponseCode()));
InputStream is = conn.getInputStream();
// retrieve the response from server
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 ){ b.append( (char)ch ); }
String s=b.toString();
Log.i("Response",s);
dos.close();
if(String.valueOf(conn.getResponseCode()).equals("200"))
{
return true;
}else{
return false;
}
}
catch (MalformedURLException ex)
{
Log.e(Tag, "URL error: " + ex.getMessage(), ex);
}
catch (IOException ioe)
{
Log.e(Tag, "IO error: " + ioe.getMessage(), ioe);
}
return false;
}
@Override
public void run() {
}
}
答案 0 :(得分:0)
您的问题仍然很模糊,但其中一个原因是 - 当您获取文件的相对URI时,但需要上传的绝对URI。
您可以查看不同的FileUtil类,例如https://github.com/z0rawarr/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/FileUtils.java
从URI获取绝对路径并使用该路径上传。 另外,不要忘记使用调试器,并在uploadFile()上应用断点来调试URI。