ADD file via upload

master
p10297854 4 years ago
parent 2df0f4761f
commit 5b684ab4a4

@ -0,0 +1,144 @@
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://10.0.2.2: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;
}
}
Loading…
Cancel
Save