parent
90c7cd78ff
commit
d05b685fa3
@ -0,0 +1,12 @@ |
||||
package com.arialyy.aria.core.inf; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/9. |
||||
*/ |
||||
|
||||
public interface ICmd { |
||||
/** |
||||
* 执行命令 |
||||
*/ |
||||
public abstract void executeCmd(); |
||||
} |
@ -0,0 +1,11 @@ |
||||
package com.arialyy.aria.core.upload; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/9. |
||||
* 上传监听 |
||||
*/ |
||||
|
||||
public interface IUploadListener { |
||||
|
||||
public void onFail(); |
||||
} |
@ -0,0 +1,150 @@ |
||||
package com.arialyy.aria.core.upload; |
||||
|
||||
import android.util.Log; |
||||
import java.io.BufferedReader; |
||||
import java.io.File; |
||||
import java.io.FileInputStream; |
||||
import java.io.IOException; |
||||
import java.io.InputStreamReader; |
||||
import java.io.OutputStream; |
||||
import java.io.OutputStreamWriter; |
||||
import java.io.PrintWriter; |
||||
import java.net.HttpURLConnection; |
||||
import java.net.URL; |
||||
import java.net.URLConnection; |
||||
|
||||
public class MultipartUtility { |
||||
|
||||
private final String boundary; |
||||
private static final String LINE_FEED = "\r\n"; |
||||
private HttpURLConnection httpConn; |
||||
private String charset; |
||||
private OutputStream outputStream; |
||||
private PrintWriter writer; |
||||
|
||||
/** |
||||
* This constructor initializes a new HTTP POST request with content type |
||||
* is set to multipart/form-data |
||||
* |
||||
* @param requestURL |
||||
* @param charset |
||||
* @throws IOException |
||||
*/ |
||||
public MultipartUtility(String requestURL, String charset) |
||||
throws IOException { |
||||
this.charset = charset; |
||||
|
||||
// creates a unique boundary based on time stamp
|
||||
boundary = "===" + System.currentTimeMillis() + "==="; |
||||
|
||||
URL url = new URL(requestURL); |
||||
Log.e("URL", "URL : " + requestURL.toString()); |
||||
httpConn = (HttpURLConnection) url.openConnection(); |
||||
httpConn.setUseCaches(false); |
||||
httpConn.setDoOutput(true); // indicates POST method
|
||||
httpConn.setDoInput(true); |
||||
httpConn.setRequestProperty("Content-Type", |
||||
"multipart/form-data; boundary=" + boundary); |
||||
httpConn.setRequestProperty("User-Agent", "CodeJava Agent"); |
||||
httpConn.setRequestProperty("Test", "Bonjour"); |
||||
outputStream = httpConn.getOutputStream(); |
||||
writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), |
||||
true); |
||||
} |
||||
|
||||
/** |
||||
* Adds a form field to the request |
||||
* |
||||
* @param name field name |
||||
* @param value field value |
||||
*/ |
||||
public void addFormField(String name, String value) { |
||||
writer.append("--" + boundary).append(LINE_FEED); |
||||
writer.append("Content-Disposition: form-data; name=\"" + name + "\"") |
||||
.append(LINE_FEED); |
||||
writer.append("Content-Type: text/plain; charset=" + charset).append( |
||||
LINE_FEED); |
||||
writer.append(LINE_FEED); |
||||
writer.append(value).append(LINE_FEED); |
||||
writer.flush(); |
||||
} |
||||
|
||||
/** |
||||
* Adds a upload file section to the request |
||||
* |
||||
* @param fieldName name attribute in <input type="file" name="..." /> |
||||
* @param uploadFile a File to be uploaded |
||||
* @throws IOException |
||||
*/ |
||||
public void addFilePart(String fieldName, File uploadFile) |
||||
throws IOException { |
||||
String fileName = uploadFile.getName(); |
||||
writer.append("--" + boundary).append(LINE_FEED); |
||||
writer.append( |
||||
"Content-Disposition: form-data; name=\"" + fieldName |
||||
+ "\"; filename=\"" + fileName + "\"") |
||||
.append(LINE_FEED); |
||||
writer.append( |
||||
"Content-Type: " |
||||
+ URLConnection.guessContentTypeFromName(fileName)) |
||||
.append(LINE_FEED); |
||||
writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED); |
||||
writer.append(LINE_FEED); |
||||
writer.flush(); |
||||
|
||||
FileInputStream inputStream = new FileInputStream(uploadFile); |
||||
byte[] buffer = new byte[4096]; |
||||
int bytesRead = -1; |
||||
while ((bytesRead = inputStream.read(buffer)) != -1) { |
||||
outputStream.write(buffer, 0, bytesRead); |
||||
} |
||||
outputStream.flush(); |
||||
inputStream.close(); |
||||
|
||||
writer.append(LINE_FEED); |
||||
writer.flush(); |
||||
} |
||||
|
||||
/** |
||||
* Adds a header field to the request. |
||||
* |
||||
* @param name - name of the header field |
||||
* @param value - value of the header field |
||||
*/ |
||||
public void addHeaderField(String name, String value) { |
||||
writer.append(name + ": " + value).append(LINE_FEED); |
||||
writer.flush(); |
||||
} |
||||
|
||||
/** |
||||
* Completes the request and receives response from the server. |
||||
* |
||||
* @return a list of Strings as response in case the server returned |
||||
* status OK, otherwise an exception is thrown. |
||||
* @throws IOException |
||||
*/ |
||||
public String finish() throws IOException { |
||||
StringBuffer response = new StringBuffer(); |
||||
|
||||
writer.append(LINE_FEED).flush(); |
||||
writer.append("--" + boundary + "--").append(LINE_FEED); |
||||
writer.close(); |
||||
|
||||
// checks server's status code first
|
||||
int status = httpConn.getResponseCode(); |
||||
if (status == HttpURLConnection.HTTP_OK) { |
||||
BufferedReader reader = new BufferedReader(new InputStreamReader( |
||||
httpConn.getInputStream())); |
||||
String line = null; |
||||
while ((line = reader.readLine()) != null) { |
||||
response.append(line); |
||||
} |
||||
reader.close(); |
||||
httpConn.disconnect(); |
||||
} else { |
||||
throw new IOException("Server returned non-OK status: " + status); |
||||
} |
||||
|
||||
return response.toString(); |
||||
} |
||||
} |
@ -0,0 +1,30 @@ |
||||
package com.arialyy.aria.core.upload; |
||||
|
||||
import com.arialyy.aria.core.download.DownloadEntity; |
||||
import com.arialyy.aria.orm.DbEntity; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/9. |
||||
* 上传文件实体 |
||||
*/ |
||||
public class UploadEntity { |
||||
|
||||
private String filePath; //文件路径
|
||||
private String fileName; //文件名
|
||||
|
||||
public String getFilePath() { |
||||
return filePath; |
||||
} |
||||
|
||||
public void setFilePath(String filePath) { |
||||
this.filePath = filePath; |
||||
} |
||||
|
||||
public String getFileName() { |
||||
return fileName; |
||||
} |
||||
|
||||
public void setFileName(String fileName) { |
||||
this.fileName = fileName; |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
package com.arialyy.aria.core.upload; |
||||
|
||||
import com.arialyy.aria.core.RequestEnum; |
||||
import java.util.HashMap; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/9. |
||||
*/ |
||||
|
||||
public class UploadTaskEntity { |
||||
public UploadEntity uploadEntity; |
||||
public RequestEnum requestEnum = RequestEnum.GET; |
||||
public String uploadUrl; //上传路径
|
||||
public String uploadKey; //文件上传需要的key
|
||||
public String contentType = "multipart/form-data"; //上传的文件类型
|
||||
public Map<String, String> headers = new HashMap<>(); |
||||
|
||||
public UploadTaskEntity(UploadEntity downloadEntity) { |
||||
this.uploadEntity = downloadEntity; |
||||
} |
||||
} |
@ -0,0 +1,122 @@ |
||||
package com.arialyy.aria.core.upload; |
||||
|
||||
import android.util.Log; |
||||
import com.arialyy.aria.util.CheckUtil; |
||||
import java.io.BufferedInputStream; |
||||
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.MalformedURLException; |
||||
import java.net.URL; |
||||
import java.util.Map; |
||||
import java.util.Set; |
||||
import java.util.UUID; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/9. |
||||
* 上传工具 |
||||
*/ |
||||
public class UploadUtil implements Runnable { |
||||
private static final String TAG = "UploadUtil"; |
||||
private UploadEntity mUploadEntity; |
||||
private UploadTaskEntity mTaskEntity; |
||||
private IUploadListener mListener; |
||||
|
||||
|
||||
public UploadUtil(UploadTaskEntity taskEntity, IUploadListener listener) { |
||||
mTaskEntity = taskEntity; |
||||
CheckUtil.checkUploadEntity(taskEntity.uploadEntity); |
||||
mUploadEntity = taskEntity.uploadEntity; |
||||
mListener = listener; |
||||
} |
||||
|
||||
public void start() { |
||||
new Thread(this).start(); |
||||
} |
||||
|
||||
@Override public void run() { |
||||
File file = new File(mUploadEntity.getFilePath()); |
||||
if (!file.exists()) { |
||||
Log.e(TAG, "【" + mUploadEntity.getFilePath() + "】,文件不存在。"); |
||||
mListener.onFail(); |
||||
return; |
||||
} |
||||
String BOUNDARY = UUID.randomUUID().toString(); // 边界标识 随机生成
|
||||
String PREFIX = "--", LINE_END = "\r\n"; |
||||
String CONTENT_TYPE = "multipart/form-data"; // 内容类型
|
||||
try { |
||||
HttpURLConnection conn = (HttpURLConnection) new URL(mTaskEntity.uploadUrl).openConnection(); |
||||
conn.setReadTimeout(5000); |
||||
conn.setConnectTimeout(5000); |
||||
conn.setDoInput(true); |
||||
conn.setDoOutput(true); |
||||
conn.setUseCaches(false); |
||||
conn.setRequestMethod("POST"); |
||||
conn.setRequestProperty("Charset", "utf-8"); // 设置编码
|
||||
conn.setRequestProperty("connection", "keep-alive"); |
||||
//conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
|
||||
conn.setRequestProperty("Content-Type", mTaskEntity.contentType + ";boundary=" + BOUNDARY); |
||||
|
||||
Set<String> keys = mTaskEntity.headers.keySet(); |
||||
for (String key : keys) { |
||||
conn.setRequestProperty(key, mTaskEntity.headers.get(key)); |
||||
} |
||||
|
||||
OutputStream outputSteam = conn.getOutputStream(); |
||||
DataOutputStream dos = new DataOutputStream(outputSteam); |
||||
StringBuilder sb = new StringBuilder(); |
||||
sb.append(PREFIX); |
||||
sb.append(BOUNDARY); |
||||
sb.append(LINE_END); |
||||
sb.append("Content-Disposition: form-data; name=\"") |
||||
.append(mTaskEntity.uploadUrl) |
||||
.append("\"; filename=\"") |
||||
.append(file.getName()) |
||||
.append("\"") |
||||
.append(LINE_END); |
||||
sb.append("Content-Type:") |
||||
.append(mTaskEntity.contentType) |
||||
.append("; charset=utf-8") |
||||
.append(LINE_END); |
||||
sb.append(LINE_END); |
||||
dos.write(sb.toString().getBytes()); |
||||
|
||||
InputStream is = new FileInputStream(file); |
||||
byte[] bytes = new byte[1024]; |
||||
int len = 0; |
||||
while ((len = is.read(bytes)) != -1) { |
||||
dos.write(bytes, 0, len); |
||||
} |
||||
is.close(); |
||||
dos.write(LINE_END.getBytes()); |
||||
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes(); |
||||
dos.write(end_data); |
||||
dos.flush(); |
||||
dos.close(); |
||||
|
||||
int res = conn.getResponseCode(); |
||||
if (res == 200) { |
||||
BufferedInputStream inputStream = new BufferedInputStream(conn.getInputStream()); |
||||
byte[] buf = new byte[1024]; |
||||
StringBuilder stringBuilder = new StringBuilder(); |
||||
while (inputStream.read(buf) > 0) { |
||||
stringBuilder.append(new String(buf, 0, buf.length)); |
||||
} |
||||
String data = stringBuilder.toString(); |
||||
Log.d(TAG, data); |
||||
//L.j(data);
|
||||
//absResponse.onResponse(data);
|
||||
} else { |
||||
//absResponse.onError("error");
|
||||
} |
||||
} catch (MalformedURLException e) { |
||||
e.printStackTrace(); |
||||
} catch (IOException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,36 @@ |
||||
package com.arialyy.simple; |
||||
|
||||
import android.view.View; |
||||
import com.arialyy.aria.core.upload.IUploadListener; |
||||
import com.arialyy.aria.core.upload.UploadEntity; |
||||
import com.arialyy.aria.core.upload.UploadTaskEntity; |
||||
import com.arialyy.aria.core.upload.UploadUtil; |
||||
import com.arialyy.simple.base.BaseActivity; |
||||
import com.arialyy.simple.databinding.ActivityUploadBinding; |
||||
|
||||
/** |
||||
* Created by Aria.Lao on 2017/2/9. |
||||
*/ |
||||
|
||||
public class upload extends BaseActivity<ActivityUploadBinding>{ |
||||
@Override protected int setLayoutId() { |
||||
return R.layout.activity_upload; |
||||
|
||||
} |
||||
|
||||
public void onClick(View view){ |
||||
UploadEntity entity = new UploadEntity(); |
||||
entity.setFilePath("/sdcard/Download/test.pdf"); |
||||
entity.setFileName("test.pdf"); |
||||
UploadTaskEntity taskEntity = new UploadTaskEntity(entity); |
||||
taskEntity.uploadUrl = "http://172.21.1.160:8080/upload"; |
||||
taskEntity.uploadKey = "file"; |
||||
UploadUtil util = new UploadUtil(taskEntity, new IUploadListener() { |
||||
@Override public void onFail() { |
||||
|
||||
} |
||||
}); |
||||
util.start(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,22 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"> |
||||
|
||||
<LinearLayout |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
android:orientation="vertical" |
||||
> |
||||
<include layout="@layout/layout_bar"/> |
||||
|
||||
<Button |
||||
android:id="@+id/single_task" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:onClick="onClick" |
||||
android:text="上传" |
||||
style="?buttonBarButtonStyle" |
||||
/> |
||||
|
||||
</LinearLayout> |
||||
|
||||
</layout> |
Loading…
Reference in new issue