Soru & Cevap

File U ...

15.12.2014 - 03:14

Merhaba arkadaşlar. Php servea dosya göndermeye çalışıyorum fakat "java.io.FileNotFoundException " hatası almaktayım. Yardımınıza ihityacım var

 

 

kodum:

[code]

package com.androidexample.uploadtoserver;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class UploadToServer extends Activity {
    
    TextView messageText;
    Button uploadButton;
    int serverResponseCode = 0;
    ProgressDialog dialog = null;       
    String upLoadServerUri = null;
    
    /**********  File Path *************/
    final String uploadFilePath = "/mnt/sdcard/";
    final String uploadFileName = "service.jpg";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_to_server);
         
        uploadButton = (Button)findViewById(R.id.uploadButton);
        messageText  = (TextView)findViewById(R.id.messageText);
                         
        
          uploadButton.setOnClickListener(new OnClickListener() {            
            @Override
            public void onClick(View v) {
                
                dialog = ProgressDialog.show(UploadToServer.this, "", "Uploading file...", true);
                
                new Thread(new Runnable() {
                        public void run() {
                             runOnUiThread(new Runnable() {
                                    public void run() {
                                        //messageText.setText("uploading started.....");
                                    }
                                });                      
                             doFileUpload();                             
                             Log.d("deneme", "buraya geldim i");                       
                        }
                      }).start();        
                }
            });
    }
    
        
        private void doFileUpload() { 
            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;           
            String existingFileName = uploadFilePath+uploadFileName;
          //  imageView1.setImageURI(Uri.parse(existingFileName));
            Log.e("--adress Debug--",existingFileName);
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary = "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            String responseFromServer ="";    
            String urlString = "http://www.*********.info/test.php";           
            Log.d("deneme1", "buraya geldim i");  
            try {             
                //------------------ CLIENT REQUEST
                FileInputStream fileInputStream = new FileInputStream(new File(existingFileName));
                // open a URL connection to the Servlet
                URL url = new URL(urlString);
                // Open a HTTP connection to the URL
                conn = (HttpURLConnection) url.openConnection();
                // Allow Inputs
                conn.setDoInput(true);
                // Allow Outputs
                conn.setDoOutput(true);
                // Don't use a cached copy.
                conn.setUseCaches(false);
                // Use a post method.
                Log.d("deneme2", "buraya geldim i");  
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                dos = new DataOutputStream(conn.getOutputStream());
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + lineEnd);
                dos.writeBytes(lineEnd);
                // create a buffer of maximum size
                Log.d("deneme3", "buraya geldim i");  
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                buffer = new byte[bufferSize];
                // read file and write it into form...
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                Log.e("--burya geldimi --","");
                while (bytesRead > 0) {
                    
                    dos.write(buffer, 0, bufferSize);
                    bytesAvailable = fileInputStream.available();
                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                    Log.e("--b----------------mi --","");
                }
                Log.d("deneme4", "buraya geldim i");  
                // send multipart form data necesssary after file data...
                dos.writeBytes(lineEnd);
                dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                // close streams
                Log.e("Debug", "File is written");
                fileInputStream.close();
                dos.flush();
                dos.close();

            } catch (MalformedURLException ex) {
                Log.e("Debug0", "error1: " + ex.getMessage(), ex);
            } catch (IOException ioe) {
                Log.e("Debug1", "error2: " + ioe.getMessage(), ioe);
            }

            //------------------ read the SERVER RESPONSE
            try {

                inStream = new DataInputStream(conn.getInputStream());
                String str;

                while ((str = inStream.readLine()) != null) {

                    Log.e("Debug2", "Server Response " + str);

                }

                inStream.close();

            } catch (IOException ioex) {
                Log.e("Debug3", "error3: " + ioex.getMessage(), ioex);
            }
        }
        
}

[/code]

1 Görüntülenme

1 Cevap

Sitedeki sorulara cevap verebilmek için giriş yapın ya da üye olun.

Geleceği Yazanlar Kullanıcısı
14.01.2015 - 05:42

Merakla takip ediyorum.
 

picture-8426-1384959619.jpg
isokdz
15.12.2014 - 03:15

LOGCAT ı

 

[code]

12-15 21:10:33.599: E/Debug3(9087): error3: http://www.****.info/test.php
12-15 21:10:33.599: E/Debug3(9087): java.io.FileNotFoundException: http://www.*****.info/test.php
12-15 21:10:33.599: E/Debug3(9087):     at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
12-15 21:10:33.599: E/Debug3(9087):     at com.androidexample.uploadtoserver.UploadToServer.doFileUpload(UploadToServer.java:140)
12-15 21:10:33.599: E/Debug3(9087):     at com.androidexample.uploadtoserver.UploadToServer.access$0(UploadToServer.java:66)
12-15 21:10:33.599: E/Debug3(9087):     at com.androidexample.uploadtoserver.UploadToServer$1$1.run(UploadToServer.java:57)
12-15 21:10:33.599: E/Debug3(9087):     at java.lang.Thread.run(Thread.java:1019)

 

[/code]