package com.example.musicwork.freeModel; import android.os.Environment; import android.util.Log; import com.example.musicwork.Data; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.util.HashMap; import com.example.musicwork.Data; import com.example.musicwork.SignIn; public class FileServiceUpload { // 分割符 public static boolean uploadForm(String filepath,String filename) throws IOException { String fileFormName="MIDI"; String newFileName=filename; File uploadFile = new File(filepath); //Log.e("filelegth:",String.valueOf(uploadFile.length())); // 普通参数 HashMap params = new HashMap<>(); params.put("username", SignIn.author_name); params.put("filename", filename); String urlStr = "http://42.193.97.132:8080/Manager/uploadfile-servlet"; MyThread_file myThread = new MyThread_file(fileFormName,newFileName,uploadFile,params,urlStr); try { myThread.start(); myThread.join(); } catch (InterruptedException e) { e.printStackTrace(); } return myThread.getResult(); } } class MyThread_file extends Thread { private static final String BOUNDARY = "----WebKitFormBoundaryT1HoybnYeFOGFlBR"; private String fileFormName; private String newFileName; private String urlStr; private File uploadFile; private HashMap params; HttpURLConnection httpURLConnection; public boolean result=false; public MyThread_file(String fileFormName, String newFileName, File uploadFile, HashMap params, String urlStr) { this.fileFormName = fileFormName; this.newFileName = newFileName; this.urlStr = urlStr; this.uploadFile = uploadFile; this.params = params; } @Override public void run() { try { StringBuilder sb = new StringBuilder(); /** * 普通的表单数据 */ 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("--" + BOUNDARY + "\r\n"); sb.append("Content-Disposition: form-data; name=\"" + fileFormName + "\"; filename=\"" + newFileName + "\"" + "\r\n"); //sb.append("Content-Type: image/jpeg" + "\r\n");// 如果服务器端有文件类型的校验,必须明确指定ContentType sb.append("\r\n"); byte[] headerInfo = sb.toString().getBytes("UTF-8"); Log.e("sb:",sb.toString()); byte[] endInfo = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8"); URL url = new URL(urlStr); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); // 设置传输内容的格式,以及长度 conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY); conn.setRequestProperty("Content-Length", String.valueOf(headerInfo.length + uploadFile.length() + endInfo.length)); conn.setDoOutput(true); //out = (DataOutputStream) conn.getOutputStream(); Log.e("file legth",String.valueOf(uploadFile.length())); InputStream in = new FileInputStream(uploadFile); OutputStream out = conn.getOutputStream(); // 写入头部 (包含了普通的参数,以及文件的标示等) out.write(headerInfo); // 写入文件 byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) != -1) { out.write(buf, 0, len); } Log.e("file legth",String.valueOf(uploadFile.length())); // 写入尾部 out.write(endInfo); in.close(); out.close(); if (conn.getResponseCode() == 200) { result=true; // System.out.println("文件上传成功"); Log.e("res",String.valueOf(result)); }else{ Log.e("res",String.valueOf(conn.getResponseCode())); } } catch (Exception e) { e.printStackTrace(); } } public boolean getResult() { return result; } public HttpURLConnection gethttpURLConnection() { return httpURLConnection; } }