You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
5.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<String , String> 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<String, String> params;
HttpURLConnection httpURLConnection;
public boolean result=false;
public MyThread_file(String fileFormName, String newFileName, File uploadFile, HashMap<String, String> 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;
}
}