【问题标题】:Uploading file onto apache server using httppost使用 httppost 将文件上传到 apache 服务器
【发布时间】:2012-07-04 07:04:21
【问题描述】:

我目前正在尝试将文件/图像从 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>

它还没有上传文件。

【问题讨论】:

    标签: java android apache apache-httpcomponents


    【解决方案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();
                }   
            }
    
        }
    

    【讨论】:

    • 嘿伙计,这太神奇了。我根本没有考虑使用在线提供的 FTP 库。非常感谢您让我使用此代码并让我了解 FTP,非常感谢。
    • 作为回报,我有幸从你那里得到一些积分作为感谢......嘿嘿嘿......
    【解决方案2】:

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

    这里有一个例子:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 2016-01-25
      相关资源
      最近更新 更多