如何将图片上传到服务器

发布于 2024-10-08 17:11:39 字数 1981 浏览 3 评论 0原文

我尝试将图像上传到服务器两天,但无法发布图像。编码已编译并成功运行,但图像未写入服务器。

这是我的编码:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Toast;

public class sde extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loadtoUrl("http://
");
    }

    private void loadtoUrl(String string) {

        // TODO Auto-generated method stub
         try {
             String pathToOurFile = "/sdcard/tamil.PNG";
             FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
             BufferedInputStream bis = new BufferedInputStream(fileInputStream,3000);
             byte[] bt=new byte[bis.available()];
            HttpURLConnection connection = (HttpURLConnection)new URL(string).openConnection();
             connection.setDoOutput(true);
             connection.setRequestMethod("POST");
             connection.connect();
            FileOutputStream input = (FileOutputStream) connection.getOutputStream();
            input.write(bt);
        } catch (MalformedURLException e) {
            Context context = null;
            int duration = 0;
            Toast.makeText(context, "erro in writing", duration);
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

i tried to upload the image to the server for two days but i could not post the image .the coding is compiled and run sucessfully but the imag is not write into the server.

this is my coding:

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.Toast;

public class sde extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loadtoUrl("http://
");
    }

    private void loadtoUrl(String string) {

        // TODO Auto-generated method stub
         try {
             String pathToOurFile = "/sdcard/tamil.PNG";
             FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile) );
             BufferedInputStream bis = new BufferedInputStream(fileInputStream,3000);
             byte[] bt=new byte[bis.available()];
            HttpURLConnection connection = (HttpURLConnection)new URL(string).openConnection();
             connection.setDoOutput(true);
             connection.setRequestMethod("POST");
             connection.connect();
            FileOutputStream input = (FileOutputStream) connection.getOutputStream();
            input.write(bt);
        } catch (MalformedURLException e) {
            Context context = null;
            int duration = 0;
            Toast.makeText(context, "erro in writing", duration);
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗫嚅 2024-10-15 17:11:39

这是一篇不错的文章, http:// blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/。它包含一个示例,该示例将上传图像并将其写入服务器端。有用。

here is a nice article, http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/. It contains an example that will upload an image and write it at server side. It works.

不…忘初心 2024-10-15 17:11:39
public boolean fileUpload(Map<String , String> params, ByteArrayOutputStream file, String link) throws Throwable{


    Account user = Util.getAccount(getApplicationContext());

    try{

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost postRequest = new HttpPost(link);

        MultipartEntity multipartContent = new MultipartEntity();

        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String , String> entry : params.entrySet()) {
                multipartContent.addPart(entry.getKey(),new StringBody(entry.getValue(),Charset.forName(HTTP.UTF_8)));
            }
        }

        byte[] data = file.toByteArray();

        ByteArrayBody img = new ByteArrayBody(data, "capture.jpg");
        multipartContent.addPart("image",img);
        postRequest.setEntity(multipartContent);
        HttpResponse res = httpClient.execute(postRequest);
        res.getEntity().getContent().close();

        return true;

    }catch(Throwable e){
        throw e;
    }

}
public boolean fileUpload(Map<String , String> params, ByteArrayOutputStream file, String link) throws Throwable{


    Account user = Util.getAccount(getApplicationContext());

    try{

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost postRequest = new HttpPost(link);

        MultipartEntity multipartContent = new MultipartEntity();

        if (params != null && !params.isEmpty()) {
            for (Map.Entry<String , String> entry : params.entrySet()) {
                multipartContent.addPart(entry.getKey(),new StringBody(entry.getValue(),Charset.forName(HTTP.UTF_8)));
            }
        }

        byte[] data = file.toByteArray();

        ByteArrayBody img = new ByteArrayBody(data, "capture.jpg");
        multipartContent.addPart("image",img);
        postRequest.setEntity(multipartContent);
        HttpResponse res = httpClient.execute(postRequest);
        res.getEntity().getContent().close();

        return true;

    }catch(Throwable e){
        throw e;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文