From b70fc2cadce19ce071abfde6149e9b21c1a23859 Mon Sep 17 00:00:00 2001 From: p10297854 <944423246@qq.com> Date: Fri, 9 Jul 2021 18:04:06 +0800 Subject: [PATCH] ADD file via upload --- main/java/recognition/Image.java | 137 +++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 main/java/recognition/Image.java diff --git a/main/java/recognition/Image.java b/main/java/recognition/Image.java new file mode 100644 index 0000000..2473c08 --- /dev/null +++ b/main/java/recognition/Image.java @@ -0,0 +1,137 @@ +package recognition; + +import android.util.Log; + +import com.example.musicwork.SignIn; + +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; + +public class Image { + public static boolean uploadImage(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/youhua/uploadfile-servlet"; + MyThread_fileImage myThread = new MyThread_fileImage(fileFormName,newFileName,uploadFile,params,urlStr); + try + { + myThread.start(); + myThread.join(); + } + catch (InterruptedException e) + { + e.printStackTrace(); + } + + return myThread.getResult(); + } + + +} +class MyThread_fileImage 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_fileImage(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; + + } +}