当前位置:首页 » 图片资讯 » 安卓开发中怎么上传图片到服务器
扩展阅读
美女健身跳河视频 2023-08-31 22:08:21
西方贵族美女照片真人 2023-08-31 22:08:15

安卓开发中怎么上传图片到服务器

发布时间: 2022-09-22 00:52:22

A. android怎么在服务器和客户端之间传输图片

android客户端和java服务端之间可以用socket来传输图片。
服务器端代码:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server02 {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(40000);
Socket socket = server.accept();
DataInputStream dos = new DataInputStream(socket.getInputStream());
int len = dos.available();
System.out.println("len = "+len);
byte[] data = new byte[len];
dos.read(data);

System.out.println("data = "+data);
dos.close();
socket.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客户端代码:
[java] view plain
imageView02 = (ImageView)findViewById(R.id.image02);
button02 = (Button)findViewById(R.id.Button02);
button02.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Socket socket;
try {
socket = new Socket("192.168.1.203",40000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qt);
imageView02.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//读取图片到ByteArrayOutputStream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
out.write(bytes);

System.out.println("bytes--->"+bytes);
out.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});

}

B. 怎样把安卓的照片上传到php的服务器

可以用php写app接口给安卓软件调用,比如写一个图片上传接口,然后在图片上传的时候将信息传入这接口就可以了。

C. 怎样把安卓的照片上传到php的服务器

安装一个上传下载的软件(如FTP),然后连接服务器,之后吧图片放到指定的文件夹即可。你可以去服务器厂商(正睿)的网上找找相关的技术文档参考一下,应该很快就清楚了!

D. 安卓如何在后台上传图片等资料

public class EX08_11 extends Activity
{
/* 变量声明
* newName:上传后在服务器上的文件名称
* uploadFile:要上传的文件路径
* actionUrl:服务器对应的程序路径 */
// private String newName="345444.jpg";
private String uploadFile="/sdcard/345444.jpg";
private String actionUrl="http://*********/upload.PHP";
private TextView mText1;
private TextView mText2;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mText1 = (TextView) findViewById(R.id.myText2);
mText1.setText("文件路径:/n"+uploadFile);

mText2 = (TextView) findViewById(R.id.myText3);
mText2.setText("上传网址:/n"+actionUrl);
/* 设定mButton的onClick事件处理 */
mButton = (Button) findViewById(R.id.myButton);
mButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
uploadFile();
}
});
}

/* 上传文件吹Server的method */
private void uploadFile()
{
// String end = "/r/n";
// String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允许Input、Output,不使用Cache */
// con.setReadTimeout(5 * 1000);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 设定传送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("enctype",
"multipart/form-data;boundary="+boundary);
/* 设定DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
/*ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=/"file1/";filename=/"" +
newName +"/"" + end);
ds.writeBytes(end); */

/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 设定每次写入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int length = -1;
/* 从文件读取数据到缓冲区 */
while((length = fStream.read(buffer)) != -1)
{
/* 将数据写入DataOutputStream中 */
ds.write(buffer, 0, length);
}
// ds.writeBytes(end);
// ds.writeBytes(twoHyphens + boundary + twoHyphens + end);

/* close streams */
fStream.close();
ds.flush();

/* 取得Response内容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 将Response显示于Dialog */
showDialog(b.toString().trim());
/* 关闭DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}

/* 显示Dialog的method */
private void showDialog(String mess)
{
new AlertDialog.Builder(EX08_11.this).setTitle("Message")
.setMessage(mess)
.setNegativeButton("确定",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
}
}

PHP代码

[php] view plain
$data = file_get_contents('php://input');
$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/image/345444.jpg', 'w');
if ($handle)
{
fwrite($handle,$data);
fclose($handle);
echo "success";
}

E. 如何调用android的拍照或本地相册选取,然后再实现相片上传服务器

首先是拍照:使用Intent即可,

[java] view plainprint?
01.final String start = Environment.getExternalStorageState();
02.private static final String PHOTOPATH = "/photo/";
03.
04.if(start.equals(Environment.MEDIA_MOUNTED)){
05.Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
06.File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
07.if(!file.exists()){
08.file.mkdirs();
09.}
10.tempphontname = System.currentTimeMillis()+".jpg";
11.buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
12.intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
13.startActivityForResult(intent, 1);
14.}
final String start = Environment.getExternalStorageState();
private static final String PHOTOPATH = "/photo/";

if(start.equals(Environment.MEDIA_MOUNTED)){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
if(!file.exists()){
file.mkdirs();
}
tempphontname = System.currentTimeMillis()+".jpg";
buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
startActivityForResult(intent, 1);
}
其次是从本地相册选:依旧是Intent.

如下代码:

[java] view plainprint?
01.if(start.equals(Environment.MEDIA_MOUNTED)){
02. Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
03. getImage.addCategory(Intent.CATEGORY_OPENABLE);
04. getImage.setType("image/jpeg");
05. startActivityForResult(getImage, 0);
06.}
if(start.equals(Environment.MEDIA_MOUNTED)){
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg");
startActivityForResult(getImage, 0);
}

接下来是主要的:因为调用完系统的方法后,回返回到回调方法onActivityResult(int, int, Intent)中,

在里面进行主要的照片上传服务器的操作,

见代码:

[java] view plainprint?
01.@Override
02. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
03. ContentResolver resolver = getContentResolver();
04. if(requestCode==1)//
05. {
06. if(resultCode==Activity.RESULT_OK)
07. {
08. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
09. {
10.ew Thread(new Runnable()
11. {
12. @Override
13. public void run() {
14.//进行上传操作
15.}
16.}.start();
转载

F. android中如何上传图片到FTP服务器

android客户端实现FTP文件需要用到 commons-net-3.0.1.jar

先将jar包复制到android libs目录下
复制以下实现代码

以下为实现代码:
/**
* 通过ftp上传文件
* @param url ftp服务器地址 如:
* @param port 端口如 :
* @param username 登录名
* @param password 密码
* @param remotePath 上到ftp服务器的磁盘路径
* @param fileNamePath 要上传的文件路径
* @param fileName 要上传的文件名
* @return
*/
public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
String returnMessage = "0";
try {
ftpClient.connect(url, Integer.parseInt(port));
boolean loginResult = ftpClient.login(username, password);
int returnCode = ftpClient.getReplyCode();
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登录成功
ftpClient.makeDirectory(remotePath);
// 设置上传目录
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
fis = new FileInputStream(fileNamePath + fileName);
ftpClient.storeFile(fileName, fis);

returnMessage = "1"; //上传成功
} else {// 如果登录失败
returnMessage = "0";
}

} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
//IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return returnMessage;
}

G. 怎样把安卓的照片上传到php的服务器

只需要一些上传工具,如(FTP等),传到指定的文件里面,然后调用即可。

H. Android 上传图片到服务器

http://192.168.1.212:8011/pd/upload/fileUpload.do;
这个是服务器地址,你图片要上传的地方。。
理论上是需要一个服务器接收你上传的图片的!
他这个demo中的url是本地的,目测是写demo的人自己写的用来测试的地址

I. android手机里面的照片能批量上传到服务器吗

这个图片存放的位置是根据你的图片来源而定的。一般是放在sdcard下的某个目录下的,
。我来给你说下思路:服务端(android手机)这边需要写个工具类,来遍历SD卡下的文件,只显示jpg和png的图片。主类中有个按钮来添加图片,还有一个按钮是用来上传图片,然后写个监听,用来接收服务端发回的消息。...服务端这边写个监听来接收客户端发来的消息,保存发过来的数据流。至于手机上能显示这张图片,只要在写个imageview,把图片资源加载上就ok啦,你可以去网上搜索一下“sd上的文件上传”,希望可以帮到你哦哦