0

我目前正在尝试从 AVD 将文件/图像上传到我的 apache 服务器。执行代码时没有出现任何错误,但图像未上传到服务器。我已经更改了 apache 服务器上文件夹的权限,该文件夹 http://10.0.2.2/images可由其他用户写入和编辑。我遇到了这个问题,请帮忙。跟随是我的代码

此代码获取图像并解码为位图。

   bmp = BitmapFactory.decodeFile(filePath);

以下是我运行以将文件上传到服务器的代码

ByteArrayOutputStream bos = new ByteArrayOutputStream();
Bitmap tmpbmp = bmp;
tmpbmp.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(url);
ByteArrayBody bab = new ByteArrayBody(data, "test.jpg");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", bab);
reqEntity.addPart("photoCaption", new StringBody("aaaa"));
postRequest.setEntity(reqEntity);
HttpResponse httpResponse = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new  InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();

            while ((sResponse = reader.readLine()) != null) {
                s = s.append(sResponse);
            }

            Log.i("wi", "Response: " + s);

        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            Log.i("wi", "Encoding failed: " + e);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            Log.i("wi", "client protocol exception!: " + e);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("wi", "IO Exception!: " + e);
        }

我收到了来自 Log.i("wi", "Response: " + s); 的回复 =

    Response: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html> <head>  
<title>Index of /images</title> </head> <body><h1>Index of /images</h1><ul><li><a 
href="/"> Parent Directory</a></li></ul></body></html>

它没有上传文件。

4

2 回答 2

2

我正在使用Apache 的 commons-net-ftp 库将我的歌曲文件上传到服务器……我正在给你代码……尝试使用这个……

 import java.io.File;

    import java.io.FileInputStream;

    import java.io.IOException;

    import org.apache.commons.net.ftp.FTP;

    import org.apache.commons.net.ftp.FTPClient;

    import android.util.Log;



    public class MyOwn {

        public void goforIt(){


            FTPClient con = null;

            try
            {
                con = new FTPClient();
                con.connect("www.mysamle.net");                 // Its dummy Address

                if (con.login("uju495", "Stevejobs!!"))
                {
                    con.enterLocalPassiveMode();                   // Very Important

                    con.setFileType(FTP.BINARY_FILE_TYPE);        //  Very Important
                    String data = "/sdcard/Vivekm4a.m4a";

                    FileInputStream in = new FileInputStream(new File(data));
                    boolean result = con.storeFile("/Ads/Vivekm4a.m4a", in);
                    in.close();
                    if (result) Log.v("upload result", "succeeded");
                    con.logout();
                    con.disconnect();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }   
        }

    }
于 2012-07-04T07:31:57.090 回答
0

似乎服务器正在返回目录列表。您需要编写服务器端脚本来接收文件。你做过吗?

这里有一个例子:

http://getablogger.blogspot.fi/2008/01/android-how-to-post-file-to-php-server.html

于 2012-07-04T07:19:53.707 回答