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

android中如何上传图片到FTP服务器,第1张

android客户端实现FTP文件需要用到 commons-net-301jar
先将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 {
ftpClientconnect(url, IntegerparseInt(port));
boolean loginResult = ftpClientlogin(username, password);
int returnCode = ftpClientgetReplyCode();
if (loginResult && FTPReplyisPositiveCompletion(returnCode)) {// 如果登录成功
ftpClientmakeDirectory(remotePath);
// 设置上传目录
ftpClientchangeWorkingDirectory(remotePath);
ftpClientsetBufferSize(1024);
ftpClientsetControlEncoding("UTF-8");
ftpCliententerLocalPassiveMode();
fis = new FileInputStream(fileNamePath + fileName);
ftpClientstoreFile(fileName, fis);

returnMessage = "1"; //上传成功
} else {// 如果登录失败
returnMessage = "0";
}
} catch (IOException e) {
eprintStackTrace();
throw new RuntimeException("FTP客户端出错!", e);
} finally {
//IOUtilscloseQuietly(fis);
try {
ftpClientdisconnect();
} catch (IOException e) {
eprintStackTrace();
throw new RuntimeException("关闭FTP连接发生异常!", e);
}
}
return returnMessage;
}

在配备Android系统的手机中,一般都配备了GPS设备。Android为我们获取GPS数据提供了很好的接口。本文来说一下如何使用Android获取GPS的经纬度。
1 从Service继承一个类。
2 创建startService()方法
3 创建endService()方法 重载onCreate方法和onDestroy方法,并在这两个方法里面来调用startService以及endService。
4 在startService中,通过getSystemService方法获取ContextLOCATION_SERVICE。
5 基于LocationListener实现一个新类。默认将重载四个方法onLocationChanged、onProviderDisabled、onProviderEnabled、onStatusChanged。对于onLocationChanged方法是我们更新最新的GPS数据的方法。一般我们的 *** 作都只需要在这里进行处理。
6 调用LocationManager的requestLocationUpdates方法,来定期触发获取GPS数据即可。在onLocationChanged函数里面可以实现我们对得到的经纬度的最终 *** 作。
7 最后在我们的Activity里面通过按钮来启动Service,停止Service。
示意代码如下:
package comoffbyegpsservice;
import androidappService;
import androidcontentContext;
import androidcontentIntent;
import androidlocationLocationListener;
import androidlocationLocationManager;
import androidosBinder;
import androidosIBinder;
import androidutilLog;
public class GPSService extends Service {
// 2000ms
private static final long minTime = 2000;
// 最小变更距离10m
private static final float minDistance = 10;
String tag = thistoString();
private LocationManager locationManager;
private LocationListener locationListener;
private final IBinder mBinder = new GPSServiceBinder();
public void startService() {
locationManager = (LocationManager) getSystemService(ContextLOCATION_SERVICE);
locationListener = new GPSServiceListener();
locationManagerrequestLocationUpdates(LocationManagerGPS_PROVIDER, minTime, minDistance,
locationListener);
}
public void endService() {
if (locationManager != null && locationListener != null) {
locationManagerremoveUpdates(locationListener);
}
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return mBinder;
}
@Override
public void onCreate() {
//
startService();
Logv(tag, "GPSService Started");
}
@Override
public void onDestroy() {
endService();
Logv(tag, "GPSService Ended");
}
public class GPSServiceBinder extends Binder {
GPSService getService() {
return GPSServicethis;
}
}
}
GPSServiceListener的实现
package comoffbyegpsservice;
import javatextDateFormat;
import javatextSimpleDateFormat;
import javautilCalendar;
import javautilGregorianCalendar;
import javautilTimeZone;
import androidlocationLocation;
import androidlocationLocationListener;
import androidlocationLocationProvider;
import androidosBundle;
import androidutilLog;
import androidwidgetToast;
public class GPSServiceListener implements LocationListener {
private static final String tag = "GPSServiceListener";
private static final float minAccuracyMeters = 35;
private static final String hostUrl = ">不知道你是不是使用的第三方框架,提交失败之后,设置再次提交,如果再次提交还是失败,你看一下是不是设置的网络超时太短造成的!你稍微改大一些试试!最好是和服务器端联调,好确定是否将数据发送到服务器,而服务器未给你返回数据


欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/zz/12730495.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2025-08-27
下一篇2025-08-27

发表评论

登录后才能评论

评论列表(0条)

    保存