android 网络文件上传下载工具类总结

1、获取文件的最后修改时间

	@SuppressLint("SimpleDateFormat")
	public String getFileDataTime(File file) {
		Date date = new Date(file.lastModified());
		SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 24小时制
		String LgTime = sdformat.format(date);
		return LgTime;
	}

2、比较两个时间的大小

	@SuppressLint("SimpleDateFormat")
	public int compareDataTime(String date1, String date2) {
		java.text.DateFormat df = new java.text.SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		java.util.Calendar c1 = java.util.Calendar.getInstance();
		java.util.Calendar c2 = java.util.Calendar.getInstance();
		try {
			c1.setTime(df.parse(date1));
			c2.setTime(df.parse(date2));
		} catch (java.text.ParseException e) {
			System.err.println("error");
		}
		int result = c1.compareTo(c2);
		if (result == 0)
			System.out.println("c1==c2");
		else if (result < 0)
			System.out.println("c1<c2");
		else
			System.out.println("c1>c2");

		return result;
	}

3、两个string数组比较,找出第二个数组与第一个数组不同的数据

	public String[] compareStringPre(String[] Source, String[] Object) {
		String[] result;
		if (Source.length >= Object.length) {
			result = new String[Source.length];
		} else {
			result = new String[Object.length];
		}
		int n = 0;
		for (int i = 0; i < Object.length; i++) {
			boolean flag = false;
			for (int j = 0; j < Source.length; j++) {
				if (Object[i].equals(Source[j])) {
					flag = true;
				}
			}
			if (!flag) {
				result[n] = Object[i];
				n++;
			}
		}
		return result;
	}

4、保存txt缓存文件

	private boolean saveCrash(String crash) {
		String fileName = "cache.txt";
		try {
			File file = new File(mstrFilePath, fileName);
			FileOutputStream fos = new FileOutputStream(file);
			fos.write(crash.toString().getBytes());
			fos.close();
		} catch (Exception e) {
			return false;
		}
		return true;
	}

5、读取txt读取缓存文件

	public String readCrashData() {
		String strDataLine = "";
		String filePath = mstrFilePath + "/cache.txt";
		File file = new File(filePath);
		if (file.exists() == false)
			return strDataLine;
		long fileSize = file.length();
		// 文件大于1M, 认为是无效数据, 直接删除
		if (fileSize >= 1 * 1024 * 1024) {
			file.delete();
			return strDataLine;
		}
		if (file.canRead() == false)
			return strDataLine;
		try {
			FileInputStream in = new FileInputStream(file);
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(in));
			strDataLine = reader.readLine();
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return strDataLine;
		} catch (IOException e) {
			e.printStackTrace();
			return strDataLine;
		}
		return strDataLine;
	}

缓存文件存储在sd卡,记得manifest加权限。。

6、 毫秒转时间格式

	public static String getLongTimeToDataTime(Long milliseconds) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return sdf.format(new Date(milliseconds * 1000L));
	}

7、格式化时间

	public static String getCurrentDataTime(String dataFormat){
		String strTimeFormat = dataFormat;
		if (strTimeFormat.isEmpty()) {
			strTimeFormat = "yyyyMMddHHmmss";
		}
		// 格式化时间
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat simpleFormat = new SimpleDateFormat(strTimeFormat);
		String strCurrentTime = simpleFormat.format(date);
		return strCurrentTime;
	}


在上一篇中讲了android 文件android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Android文件至服务器和下载文件至本地,亲测有效,只需入相关参数即可。/** * android文件到服务器 * * @param file * 需要上文件 * @param RequestURL * 请求的rul * @return 返回响应的内容 */ public static String uploadFile(Map<String,String>params,File file, String RequestURL) { String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成 String PREFIX = "--", LINE_END = "\r\n"; String CONTENT_TYPE = "multipart/form-data"; // 内容类型 try { URL url = new URL(RequestURL); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(TIME_OUT); conn.setConnectTimeout(TIME_OUT); conn.setDoInput(true); // 允许输入流 conn.setDoOutput(true); // 允许输出流 conn.setUseCaches(false); // 不允许使用缓存 conn.setRequestMethod("POST"); // 请求方式 conn.setRequestProperty("Charset", CHARSET); // 设置编码 conn.setRequestProperty("connection", "keep-alive"); conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY); if (file != null) { Log.i(TAG, "====file is"+file); /** * 当文件不为空,把文件包装并且上 */ OutputStream outputSteam = conn.getOutputStream(); DataOutputStream dos = new DataOutputStream(outputSteam); StringBuffer sb = new StringBuffer(); /************************上表单的一些设置信息***********************************/ if (params != null) for (String key : params.keySet()) { sb.append("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data; name=\"" + key + "\"" + "\r\n"); sb.append("\r\n"); sb.append(params.get(key) + "\r\n"); } /************************上文件的一些设置信息***********************************/ sb.append(PREFIX); sb.append(BOUNDARY); sb.append(LINE_END); /** * 这里重点注意: name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件 * filename是文件的名字,包含后缀名的 比如:abc.p

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值