如何实现Android平台上的长尾关键词文件上传功能?
- 内容介绍
- 文章标签
- 相关推荐
本文共计308个文字,预计阅读时间需要2分钟。
javaAndroid中使用okhttp3实现文件上传package com.halo.indiana.app.utils;
import java.io.File;import java.io.IOException;import okhttp3.MediaType;import okhttp3.MultipartBody;import okhttp3.OkHttpClient;import okhttp3.Request;
/** * 文件上传 */ public void uploadFile(){ boolean isSuccess = false; String image_url = ""; File file = null; try { getFiles(); String userId = getPara("userId"); TUser user = TUser.dao.findById(userId); if (user == null) { renderJson(result.addError("此用户不存在")); return; } UploadFile uploadFile = getFile("file"); file = uploadFile.getFile(); //获取文件名称 String fileName=uploadFile.getFileName(); String suffix=fileName.substring(fileName.indexOf(".")); //获取上传文件的路径 String uploadPath= PropKit.get("static_root"); String domain_static= PropKit.get("domain_static"); String newName = StringUtils.getUUID().concat(suffix); String dir = HashKit.md5(userId).substring(0, 16); StringBuffer dirPath = new StringBuffer(); dirPath.append(uploadPath).append(File.separator).append(dir); File directory = new File(dirPath.toString()); if (!directory.exists()) { directory.mkdirs(); } File newFile = new File(dirPath.toString(),newName); if (!newFile.exists()) { newFile.createNewFile(); } isSuccess = file.renameTo(newFile); System.out.println("文件路径>"+dirPath.toString()+ " "+isSuccess); log.info("文件路径>"+dirPath.toString()); if (isSuccess) { image_url = domain_static.concat(dir).concat("/").concat(newName); user.set("image_url", image_url); user.set("updateDate", new Date()); isSuccess = user.update(); } if (!isSuccess) { renderJson(result.addError("文件上传失败")); return; } renderJson(result.success(image_url)); return; } catch (Exception e) { e.printStackTrace(); result.addError("系统异常"+e.getMessage()); renderJson(result); }finally { if (file!=null && file.exists()) { file.delete(); } } }
本文共计308个文字,预计阅读时间需要2分钟。
javaAndroid中使用okhttp3实现文件上传package com.halo.indiana.app.utils;
import java.io.File;import java.io.IOException;import okhttp3.MediaType;import okhttp3.MultipartBody;import okhttp3.OkHttpClient;import okhttp3.Request;
/** * 文件上传 */ public void uploadFile(){ boolean isSuccess = false; String image_url = ""; File file = null; try { getFiles(); String userId = getPara("userId"); TUser user = TUser.dao.findById(userId); if (user == null) { renderJson(result.addError("此用户不存在")); return; } UploadFile uploadFile = getFile("file"); file = uploadFile.getFile(); //获取文件名称 String fileName=uploadFile.getFileName(); String suffix=fileName.substring(fileName.indexOf(".")); //获取上传文件的路径 String uploadPath= PropKit.get("static_root"); String domain_static= PropKit.get("domain_static"); String newName = StringUtils.getUUID().concat(suffix); String dir = HashKit.md5(userId).substring(0, 16); StringBuffer dirPath = new StringBuffer(); dirPath.append(uploadPath).append(File.separator).append(dir); File directory = new File(dirPath.toString()); if (!directory.exists()) { directory.mkdirs(); } File newFile = new File(dirPath.toString(),newName); if (!newFile.exists()) { newFile.createNewFile(); } isSuccess = file.renameTo(newFile); System.out.println("文件路径>"+dirPath.toString()+ " "+isSuccess); log.info("文件路径>"+dirPath.toString()); if (isSuccess) { image_url = domain_static.concat(dir).concat("/").concat(newName); user.set("image_url", image_url); user.set("updateDate", new Date()); isSuccess = user.update(); } if (!isSuccess) { renderJson(result.addError("文件上传失败")); return; } renderJson(result.success(image_url)); return; } catch (Exception e) { e.printStackTrace(); result.addError("系统异常"+e.getMessage()); renderJson(result); }finally { if (file!=null && file.exists()) { file.delete(); } } }

