Android 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;import okhttp3.Req
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; import okhttp3.RequestBody; import okhttp3.Response; public class UploadHelper { public static final String TAG = "UploadHelper"; private static final MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png"); private final OkHttpClient client = new OkHttpClient(); public String upload(String userId, File file) { RequestBody fileBody = RequestBody.create(MediaType.parse("image/png"), file); RequestBody requestBody = new MultipartBody.Builder() .setType(MultipartBody.FORM) /// .addPart( // Headers.of("Content-Disposition", "form-data; name=\"file\"; filename=\"" + fileName + "\""), // RequestBody.create(MEDIA_TYPE_PNG, file)) // .addPart( // Headers.of("Content-Disposition", "form-data; name=\"imagetype\""), // RequestBody.create(null, imageType)) // .addPart( // Headers.of("Content-Disposition", "form-data; name=\"userphone\""), // RequestBody.create(null, userPhone)) .addFormDataPart("file", "testfilename.png", fileBody) .addFormDataPart("userId", userId) .build(); Request request = new Request.Builder() .url("http://mp.scp-cctv.com:8088/EYIZCO/api/uploadFile") .post(requestBody) .build(); Response response; try { response = client.newCall(request).execute(); String jsonString = response.body().string(); System.out.println("reslut :"+jsonString); if (!response.isSuccessful()) { System.out.println("error..."); } else { System.out.println("success..."); } } catch (IOException e) { e.printStackTrace(); System.out.println("upload IOException "+e.getMessage()); } return null; } }Jfinal文件上传后台实现
/** * 文件上传 */ 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(); } } }