如何上传图片到服务器 [英] how to upload the image into server
本文介绍了如何上传图片到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将图像上传到服务器了两天,但我无法发布.the编码编制的形象,成功地运行,但IMAG没有写入到服务器。
这是我的编码:
进口java.io.BufferedInputStream中;
进口的java.io.File;
进口java.io.FileInputStream中;
进口java.io.FileOutputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口java.net.HttpURLConnection中;
进口java.net.MalformedURLException;
进口的java.net.URL;
进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;
进口android.widget.Toast;
公共类SDE延伸活动{
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
loadtoUrl(HTTP://
);
}
私人无效loadtoUrl(字符串字符串){
// TODO自动生成方法存根
尝试 {
字符串pathToOurFile =/sdcard/tamil.PNG;
的FileInputStream的FileInputStream =新的FileInputStream(新文件(pathToOurFile));
的BufferedInputStream双=新的BufferedInputStream(的FileInputStream,3000);
byte []的BT =新的字节[bis.available()];
HttpURLConnection的连接=(HttpURLConnection类)新的URL(字符串).openConnection();
connection.setDoOutput(真正的);
connection.setRequestMethod(POST);
connection.connect();
FileOutputStream中输入=(FileOutputStream中)connection.getOutputStream();
input.write(BT);
}赶上(MalformedURLException异常E){
上下文的背景下= NULL;
INT持续时间= 0;
Toast.makeText(背景下,埃罗以书面形式,持续时间);
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
}
}
解决方案
在这里是一个很好的文章,的 http://blog.sptechnolab.com/2011/03/09/android/android-upload-image-to-server/ 。它包含了将上传的图片,并在服务器端把它写的一个例子。它的工作原理。
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();
}
}
}
解决方案
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.
这篇关于如何上传图片到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文