android通过http上传文件,服务器端用php写(原创)

简介: android通过http上传文件,服务器端用php写(原创)

服务器端php代码

<?php
///如果有上传文件则接收
if($_FILES){
$target_path = $target_path . basename( $_FILES['file1']['name']);
try{ if(move_uploaded_file($_FILES['file1']['tmp_name'], $target_path)) {
echo "The file ".  basename( $_FILES['file1']['name']). " has been uploaded";
}
} catch( Exception $e ) {
echo $e->getMessage();   }
}
?>

Android 代码:

package com.nbcio.baishicha.test;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class test extends Activity {
/*
* 变量声明 newName:上传后在服务器上的文件名称
* 
* uploadFile:要上传的文件路径 actionUrl:服务器对应的程序路径
*/
private String newName = "";
private String uploadFile = "";
private String actionUrl = "https://www.nbcio.com/android/upload/upload.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
newName = bundle.getString("fileName");
uploadFile = newName;
try {
String ok = post(actionUrl, newName);
Toast.makeText(this, "OK!", Toast.LENGTH_LONG).show();
finish();
} catch (IOException e) {
//
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* 上传文件到Server的方法 */
/**
* 
* @param actionUrl
* @param params
* @param files
* @return
* @throws IOException
*/
public static String post(String actionUrl, String FileName)
throws IOException {
String BOUNDARY = java.util.UUID.randomUUID().toString();
String PREFIX = "--", LINEND = "\r\n";
String MULTIPART_FROM_DATA = "multipart/form-data";
String CHARSET = "UTF-8";
URL uri = new URL(actionUrl);
HttpURLConnection conn = (HttpURLConnection) uri.openConnection();
conn.setReadTimeout(5 * 1000);
// 缓存的最长时间
conn.setDoInput(true);// 允许输入
conn.setDoOutput(true);// 允许输出
conn.setUseCaches(false); // 不允许使用缓存
conn.setRequestMethod("POST");
conn.setRequestProperty("connection", "keep-alive");
conn.setRequestProperty("Charsert", "UTF-8");
conn.setRequestProperty("Content-Type", MULTIPART_FROM_DATA
+ ";boundary=" + BOUNDARY);
DataOutputStream outStream = new DataOutputStream(
conn.getOutputStream());
// 发送文件数据
if (FileName != "") {
StringBuilder sb1 = new StringBuilder();
sb1.append(PREFIX);
sb1.append(BOUNDARY);
sb1.append(LINEND);
sb1.append("Content-Disposition: form-data; name=\"file1\"; filename=\""
+ FileName + "\"" + LINEND);
sb1.append("Content-Type: application/octet-stream; charset="
+ CHARSET + LINEND);
sb1.append(LINEND);
outStream.write(sb1.toString().getBytes());
InputStream is = new FileInputStream(FileName);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = is.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
is.close();
outStream.write(LINEND.getBytes());
}
// 请求结束标志
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINEND).getBytes();
outStream.write(end_data);
outStream.flush();
// 得到响应码
int res = conn.getResponseCode();
InputStream in = null;
if (res == 200) {
in = conn.getInputStream();
int ch;
StringBuilder sb2 = new StringBuilder();
while ((ch = in.read()) != -1) {
sb2.append((char) ch);
}
}
return in == null ? null : in.toString();
}
}
相关文章
|
18天前
|
Ubuntu 网络协议 Java
【Android平板编程】远程Ubuntu服务器code-server编程写代码
【Android平板编程】远程Ubuntu服务器code-server编程写代码
|
18天前
|
缓存 网络协议 数据可视化
WinSCP下载安装并实现远程SSH本地服务器上传文件
WinSCP下载安装并实现远程SSH本地服务器上传文件
|
17天前
|
监控 Unix 应用服务中间件
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
Android-音视频学习系列-(八)基于-Nginx-搭建(rtmp、http)直播服务器
|
18天前
|
JSON Android开发 数据格式
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
android与Web服务器交互时的cookie使用-兼谈大众点评数据获得(原创)
28 2
|
18天前
|
Ubuntu Android开发 数据安全/隐私保护
【Android平板编程】远程Ubuntu服务器Code-Server编程写代码
【Android平板编程】远程Ubuntu服务器Code-Server编程写代码
|
18天前
|
网络协议 安全 Linux
如何使用Android手机通过JuiceSSH远程访问本地Linux服务器
如何使用Android手机通过JuiceSSH远程访问本地Linux服务器
|
18天前
|
监控 安全 容灾
PHP服务器稳定性保障
确保PHP服务器稳定性,需关注以下几点:配置合适硬件及优化操作系统;使用最新稳定版PHP,及时更新安全补丁;编写高质量代码并优化性能;处理异常,记录日志以便监控;管理资源,使用性能监控工具;加强安全防护,如权限设置、防注入攻击;采用自动化部署和持续集成工具;定期备份数据,建立容灾机制。
22 0
|
18天前
|
Ubuntu 网络协议 Java
在Android平板上使用code-server公网远程Ubuntu服务器编程
在Android平板上使用code-server公网远程Ubuntu服务器编程
|
18天前
|
弹性计算 PHP
ECS续费问题之PHP运行环境到期如何解决
ECS续费是指对已创建的阿里云ECS实例执行的续费操作,以延长其服务期限;本合集旨在为用户提供ECS续费的操作步骤、策略选择和注意事项,确保业务的持续性和成本的优化。
ECS续费问题之PHP运行环境到期如何解决
|
5天前
|
弹性计算 监控 开发工具
【阿里云弹性计算】阿里云ECS的网络优化实践:VPC配置与网络性能提升
【5月更文挑战第29天】阿里云ECS通过虚拟私有云(VPC)提供高性能、安全的网络环境。VPC允许用户自定义IP地址、路由规则和安全组。配置包括:创建VPC和交换机,设定安全组,然后创建ECS实例并绑定。优化网络性能涉及规划网络拓扑、优化路由、启用网络加速功能(如ENI和EIP)及监控网络性能。示例代码展示了使用Python SDK创建VPC和交换机的过程。
32 3