使用httppost将文件上传到apache服务器

时间:2012-07-04 07:04:22

标签: java android apache avd apache-httpcomponents

每当我问你问题时,都要提前帮助我。顺便说一下,这不是与家庭作业相关的问题。我目前正在尝试从AVD上传文件/图像到我的apache服务器。我执行代码时没有出现任何错误,但图像没有上传到服务器。我已经更改了apache服务器上文件夹的权限 http://10.0.2.2/images可由其他用户编写和编辑。我遇到这个问题=(PLZ帮助。关注是我的代码

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

   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”,“回复:”+ 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>

尚未上传文件=(

谢谢你们帮助我

2 个答案:

答案 0 :(得分:1)

我正在使用 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();
            }   
        }

    }

答案 1 :(得分:0)

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

这里有一个例子:

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