Skip to content

Commit

Permalink
update client upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreampie committed Jan 5, 2016
1 parent e8de8d9 commit f4287f3
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 26 deletions.
39 changes: 18 additions & 21 deletions resty-client/src/main/java/cn/dreampie/client/ClientConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import cn.dreampie.client.exception.ClientException;
import cn.dreampie.common.http.ContentType;
import cn.dreampie.common.http.HttpMethod;
import cn.dreampie.common.util.HttpTyper;
import cn.dreampie.common.util.stream.DefaultFileRenamer;
import cn.dreampie.common.util.stream.FileRenamer;
import cn.dreampie.log.Logger;

import javax.net.ssl.*;
import java.io.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.*;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -120,7 +122,7 @@ protected HttpURLConnection getHttpConnection(String httpMethod) throws IOExcept
//上传文件类型
conn = getStreamConnection(httpMethod, clientRequest);
//是上传文件
Map<String, String> uploadFiles = clientRequest.getUploadFiles();
Map<String, ClientFile> uploadFiles = clientRequest.getUploadFiles();
if (uploadFiles != null && uploadFiles.size() > 0) {
String boundary = "---------------------------" + getRandomString(13); //boundary就是request头和上传文件内容的分隔符
conn.setRequestProperty("Content-Type", contentType + "boundary=" + boundary);
Expand All @@ -143,7 +145,7 @@ protected HttpURLConnection getHttpConnection(String httpMethod) throws IOExcept
writer.write(builder.toString().getBytes());
}
//上传文件
writeUploadFiles(boundary, clientRequest.getUploadFiles(), writer);
writeUploadFiles(boundary, uploadFiles, writer);

byte[] endData = ("\r\n--" + boundary + "--\r\n").getBytes();
writer.write(endData);
Expand Down Expand Up @@ -210,26 +212,21 @@ private void outputParam(HttpURLConnection conn, String method, String requestPa
/**
* 写入文件到 服务器
*
* @param boundary 分隔符
* @param params 文件集合
* @param writer 写入对象
* @param boundary 分隔符
* @param uploadFiles 文件集合
* @param writer 写入对象
* @throws IOException
*/
private void writeUploadFiles(String boundary, Map<String, String> params, DataOutputStream writer) throws IOException {
private void writeUploadFiles(String boundary, Map<String, ClientFile> uploadFiles, DataOutputStream writer) throws IOException {
// file
String value = null;
for (String key : params.keySet()) {
value = params.get(key);
if (value == null) continue;
File file = new File(value);
if (!file.exists())
throw new FileNotFoundException("File not found " + file.getPath());

String filename = file.getName();
String contentType = HttpTyper.getContentTypeFromExtension(filename);
writer.write(("\r\n" + "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"" + key + "\"; filename=\"" + filename + "\"\r\n" + "Content-Type:" + contentType + "\r\n\r\n").getBytes());

DataInputStream in = new DataInputStream(new FileInputStream(file));
ClientFile clientFile;
for (String key : uploadFiles.keySet()) {
clientFile = uploadFiles.get(key);
if (clientFile == null) continue;

writer.write(("\r\n" + "--" + boundary + "\r\n" + "Content-Disposition: form-data; name=\"" + key + "\"; filename=\"" + clientFile.getName() + "\"\r\n" + "Content-Type:" + clientFile.getMimeType() + "\r\n\r\n").getBytes());

DataInputStream in = new DataInputStream(clientFile.getInputStream());
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
Expand Down
46 changes: 46 additions & 0 deletions resty-client/src/main/java/cn/dreampie/client/ClientFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package cn.dreampie.client;

import cn.dreampie.common.util.Checker;
import cn.dreampie.common.util.HttpTyper;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
* Created by Dreampie on 16/1/5.
*/
public class ClientFile {
private String name;
private String mimeType;
private InputStream inputStream;

public ClientFile(String filepath) throws FileNotFoundException {
this(new File(filepath));
}

public ClientFile(File file) throws FileNotFoundException {
this.name = file.getName();
this.mimeType = HttpTyper.getContentTypeFromExtension(name);
this.inputStream = new FileInputStream(file);
}

public ClientFile(String name, String mimeType, InputStream inputStream) {
this.name = name;
this.mimeType = mimeType;
this.inputStream = Checker.checkNotNull(inputStream);
}

public String getName() {
return name;
}

public String getMimeType() {
return mimeType;
}

public InputStream getInputStream() {
return inputStream;
}
}
32 changes: 27 additions & 5 deletions resty-client/src/main/java/cn/dreampie/client/ClientRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import cn.dreampie.common.util.Checker;
import cn.dreampie.common.util.Maper;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
Expand All @@ -24,7 +27,7 @@ public class ClientRequest {
private int readTimeOut = 10000;
private boolean overwrite = false;
private String downloadFile;
private Map<String, String> uploadFiles = Maper.of();
private Map<String, ClientFile> uploadFiles = Maper.of();
private String contentType = ContentType.FORM + ";charset=" + encoding;
private String userAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";

Expand Down Expand Up @@ -175,18 +178,37 @@ public ClientRequest setDownloadFile(String downloadFile) {
return this;
}

public Map<String, String> getUploadFiles() {
public Map<String, ClientFile> getUploadFiles() {
return uploadFiles;
}

public ClientRequest setUploadFiles(Map<String, String> uploadFiles) {
public ClientRequest setUploadFiles(Map<String, ClientFile> uploadFiles) {
this.uploadFiles = uploadFiles;
setContentType(ContentType.MULTIPART + ";charset=" + encoding);
return this;
}

public ClientRequest addUploadFile(String name, String filepath) {
this.uploadFiles.put(name, filepath);
public ClientRequest addUploadFile(String name, String filepath) throws FileNotFoundException {
this.uploadFiles.put(name, new ClientFile(filepath));
setContentType(ContentType.MULTIPART + ";charset=" + encoding);
return this;
}

public ClientRequest addUploadFile(String name, File file) throws FileNotFoundException {
this.uploadFiles.put(name, new ClientFile(file));
setContentType(ContentType.MULTIPART + ";charset=" + encoding);
return this;
}

/**
* @param name param name
* @param filename file name
* @param mimeType
* @param fileStream
* @return
*/
public ClientRequest addUploadFile(String name, String filename, String mimeType, InputStream fileStream) {
this.uploadFiles.put(name, new ClientFile(filename, mimeType, fileStream));
setContentType(ContentType.MULTIPART + ";charset=" + encoding);
return this;
}
Expand Down

0 comments on commit f4287f3

Please sign in to comment.