第一步 首先是要文件传输,类似于文件传输的代码网上一大把
/**
* 上传文件到静态资源服务器
* @param fileType 文件类型(1:图片;2:文件;3:语音)
* @param is 图片文件流
* @param filename 文件名
* @param fileDir 文件目录(模块名/功能名;如:"usercenter/user")
* @param isResize 是否压缩图片
* @param subFileName 子文件名
* @return
*/
@SuppressWarnings("unchecked")
public static String uploadFileToServer(String fileType, InputStream is, String filename, String fileDir,
boolean isResize, String subFileName) {
String res = "";
HttpURLConnection conn = null;
// boundary就是request头和上传文件内容的分隔符
String BOUNDARY = "--------------------------------";
try {
String urlType = "";
if ("1".equals(fileType)) {
urlType = UPLOAD_IMAGE;
} else if ("2".equals(fileType)) {
urlType = UPLOAD_FILE;
} else if ("3".equals(fileType)) {
urlType = UPLOAD_VOICE;
}
URL url = new URL(getServerUploadUrl() + urlType);
conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv