首页 文章 精选 留言 我的

精选列表

搜索[Web托管],共10000篇文章
优秀的个人博客,低调大师

Android上传文件到Web服务器,PHP接收文件

Android上传文件到服务器,通常采用构造http协议的方法,模拟网页POST方法传输文件,服务器端可以采用JavaServlet或者PHP来接收要传输的文件。使用JavaServlet来接收文件的方法比较常见,在这里给大家介绍一个简单的服务器端使用PHP语言来接收文件的例子。 服务器端代码比较简单,接收传输过来的文件: [php] view plain copy <?php $target_path="./upload/";//接收文件目录 $target_path=$target_path.basename($_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'],$target_path)){ echo"Thefile".basename($_FILES['uploadedfile']['name'])."hasbeenuploaded"; }else{ echo"Therewasanerroruploadingthefile,pleasetryagain!".$_FILES['uploadedfile']['error']; } ?> 手机客户端代码: [java] view plain copy packagecom.figo.uploadfile; importjava.io.BufferedReader; importjava.io.DataOutputStream; importjava.io.FileInputStream; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.net.HttpURLConnection; importjava.net.URL; importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.Button; importandroid.widget.TextView; importandroid.widget.Toast; publicclassUploadfileActivityextendsActivity { //要上传的文件路径,理论上可以传输任何文件,实际使用时根据需要处理 privateStringuploadFile="/sdcard/testimg.jpg"; privateStringsrcPath="/sdcard/testimg.jpg"; //服务器上接收文件的处理页面,这里根据需要换成自己的 privateStringactionUrl="http://10.100.1.208/receive_file.php"; privateTextViewmText1; privateTextViewmText2; privateButtonmButton; @Override publicvoidonCreate(BundlesavedInstanceState) { 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(newView.OnClickListener() { @Override publicvoidonClick(Viewv) { uploadFile(actionUrl); } }); } /*上传文件至Server,uploadUrl:接收文件的处理页面*/ privatevoiduploadFile(StringuploadUrl) { Stringend="\r\n"; StringtwoHyphens="--"; Stringboundary="******"; try { URLurl=newURL(uploadUrl); HttpURLConnectionhttpURLConnection=(HttpURLConnection)url .openConnection(); //设置每次传输的流大小,可以有效防止手机因为内存不足崩溃 //此方法用于在预先不知道内容长度时启用没有进行内部缓冲的HTTP请求正文的流。 httpURLConnection.setChunkedStreamingMode(128*1024);//128K //允许输入输出流 httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(true); httpURLConnection.setUseCaches(false); //使用POST方法 httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("Connection","Keep-Alive"); httpURLConnection.setRequestProperty("Charset","UTF-8"); httpURLConnection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); DataOutputStreamdos=newDataOutputStream( httpURLConnection.getOutputStream()); dos.writeBytes(twoHyphens+boundary+end); dos.writeBytes("Content-Disposition:form-data;name=\"uploadedfile\";filename=\"" +srcPath.substring(srcPath.lastIndexOf("/")+1) +"\"" +end); dos.writeBytes(end); FileInputStreamfis=newFileInputStream(srcPath); byte[]buffer=newbyte[8192];//8k intcount=0; //读取文件 while((count=fis.read(buffer))!=-1) { dos.write(buffer,0,count); } fis.close(); dos.writeBytes(end); dos.writeBytes(twoHyphens+boundary+twoHyphens+end); dos.flush(); InputStreamis=httpURLConnection.getInputStream(); InputStreamReaderisr=newInputStreamReader(is,"utf-8"); BufferedReaderbr=newBufferedReader(isr); Stringresult=br.readLine(); Toast.makeText(this,result,Toast.LENGTH_LONG).show(); dos.close(); is.close(); }catch(Exceptione) { e.printStackTrace(); setTitle(e.getMessage()); } } } 在AndroidManifest.xml文件里添加网络访问权限: [plain] view plain copy <uses-permissionandroid:name="android.permission.INTERNET"/> 运行结果:

优秀的个人博客,低调大师

【AI 智能运维平台】本周更新:新增多款数据库 AI tools,增强数据库智能托管能力

Blueking Lite 是一个AI First的轻量版运维产品,具有部署资源要求低、使用成本低、渐进式体验等特点,为运维管理员提供日常运维中的必备工具。 📢 【平台 · 本周更新速递】 🖥 节点管理 • 重构控制器安装流程,解决因体积过大导致的安装超时问题 📦 作业管理 • 上传 Playbook 时支持模板下载 🗂 CMDB • 新增主机配置文件采集能力 📊 监控系统 • 支持自定义 SNMP 采集模板 • 新增 etcd 监控能力 🚨 告警中心 • 新增 K8s Event 接入通道 📈 运营分析 • 内置 告警中心与CMDB 分析仪表盘 🤖 OpsPilot • 工具支持配置多个实例 • 新增 MySQL、Oracle 工具

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码