之前公司项目里需要一个文件分片上传的功能。当时随便在网上找了一个放进去。实现方式是:
这个实现步骤很鸡肋,对文件做了两次分片。我重新开发了一遍,实现方式:
那就上代码吧。
PartUploadController
类package com.mla.controller;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import com.github.pagehelper.util.StringUtil;
import com.mla.common.CacheData;
import com.mla.common.RedisConstant;
import com.mla.common.ReturnResult;
import com.mla.util.AliyunOSSClientUtil;
import com.mla.util.OSSClientConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.*;
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/part")
public class PartUploadController {
/**
* 分片上传准备
*
* @param fileTypeName
* @return
*/
@RequestMapping("/ossPartReady")
public ReturnResult ossPartReady(String fileTypeName) {
OSSClient client = AliyunOSSClientUtil.getOSSClient();
String objectKey = String.format("%s%s.%s", OSSClientConstants.PICLOCATION, UUID.randomUUID().toString().replace("-", ""), fileTypeName);
String uploadId = initMultipartUpload(client, OSSClientConstants.BACKET_NAME, objectKey);
String tagId = UUID.randomUUID().toString().replace("-", "");
Map<String, Object> map = new HashMap<>();
map.put("objectKey", objectKey);
map.put("uploadId", uploadId);
map.put("tagId", tagId);
return ReturnResult.ok(map);
}
/**
* 分片上传阿里云,前端途径后台上传阿里云oss,后端不做任何处理。
*
* @param file
* @param objectKey
* @param partNumber
* @param uploadId
* @param partCount
* @param tagId
* @return
*/
@RequestMapping("/ossPartUpload")
public ReturnResult ossPartUpload(@RequestParam MultipartFile file,
@RequestParam String objectKey,
@RequestParam int partNumber,
@RequestParam String uploadId,
@RequestParam int partCount,
@RequestParam String tagId) {
try {
if (StringUtil.isEmpty(uploadId)) {
return ReturnResult.error("上传uploadId不能为空");
}
if (StringUtil.isEmpty(tagId)) {
return ReturnResult.error("上传tagId不能为空");
}
if (StringUtil.isEmpty(objectKey)) {
return ReturnResult.error("上传objectKey不能为空");
}
OSSClient client = AliyunOSSClientUtil.getOSSClient();
UploadPartRequest uploadPartRequest = new UploadPartRequest();
uploadPartRequest.setBucketName(OSSClientConstants.BACKET_NAME);
uploadPartRequest.setKey(objectKey);
uploadPartRequest.setUploadId(uploadId);
uploadPartRequest.setInputStream(file.getInputStream());
uploadPartRequest.setPartSize(file.getSize());
uploadPartRequest.setPartNumber(partNumber);
UploadPartResult uploadPartResult = client.uploadPart(uploadPartRequest);
//将返回信息存入缓存,本来是想存入redis,取出来时解析不来,无法强转为PartETag。
CacheData.addETag(RedisConstant.PARTETAGID + tagId, uploadPartResult.getPartETag());
List<PartETag> eTags = CacheData.getETag(RedisConstant.PARTETAGID + tagId);
//最后一条上传后合并
if (eTags.size() == partCount) {
completeMultipartUpload(client, OSSClientConstants.BACKET_NAME, objectKey, uploadId, eTags);
CacheData.delETag(RedisConstant.PARTETAGID + tagId);
}
String fileUrl = String.format("%s%s", OSSClientConstants.PARTURL, objectKey);
return ReturnResult.ok(fileUrl);
} catch (IOException e) {
e.printStackTrace();
}
return ReturnResult.error();
}
// 初始化一个Multi-part upload请求。
private static String initMultipartUpload(OSSClient client, String bucketName, String key) throws OSSException, ClientException {
InitiateMultipartUploadRequest initUploadRequest = new InitiateMultipartUploadRequest(bucketName, key);
InitiateMultipartUploadResult initResult = client.initiateMultipartUpload(initUploadRequest);
return initResult.getUploadId();
}
// 完成一个multi-part请求。
private static void completeMultipartUpload(OSSClient client, String bucketName, String key, String uploadId, List<PartETag> eTags) throws OSSException, ClientException {
// 为part按partnumber排序
eTags.sort(new Comparator<PartETag>() {
public int compare(PartETag arg0, PartETag arg1) {
return arg0.getPartNumber() - arg1.getPartNumber();
}
});
CompleteMultipartUploadRequest completeMultipartUploadRequest = new CompleteMultipartUploadRequest(bucketName, key, uploadId, eTags);
client.completeMultipartUpload(completeMultipartUploadRequest);
}
}
这里是因为业务需要,写成了两个接口。其实可以一个接口搞得的
package com.mla.common;
import com.aliyun.oss.model.PartETag;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class CacheData {
private static ConcurrentHashMap<String, List<PartETag>> eTagMaps = new ConcurrentHashMap<>();
public synchronized static void addETag(String partId, PartETag eTag) {
List<PartETag> eTags = eTagMaps.get(partId);
if (eTags == null) {
eTags = new ArrayList<>();
}
eTags.add(eTag);
eTagMaps.put(partId, eTags);
}
public synchronized static List<PartETag> getETag(String partId) {
return eTagMaps.get(partId);
}
public static void delETag(String partId) {
eTagMaps.remove(partId);
}
}
AliyunOSSClientUtil
工具类,对OSS的封装。package com.mla.util;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.stream.FileImageInputStream;
import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.Random;
/**
* @author qf
* @ClassName: AliyunOSSClientUtil
* @Description: 阿里云OSS上传文件模块
* @date 2017年5月6日 下午6:01:46
*/
@Component
public class AliyunOSSClientUtil {
// log日志
private static Logger logger = Logger.getLogger(AliyunOSSClientUtil.class);
// 阿里云API的内或外网域名
private static String ENDPOINT;
// 阿里云API的密钥Access Key ID
private static String ACCESS_KEY_ID;
// 阿里云API的密钥Access Key Secret
private static String ACCESS_KEY_SECRET;
// 阿里云API的bucket名称
public static String BACKET_NAME;
// 阿里云API的文件夹名称
private static String VIOLATION;
private static String FORMAT;
private static String FORMATS;
private static String FOLDER_VIDEO;
// 初始化属性
static {
ENDPOINT = OSSClientConstants.ENDPOINT;
ACCESS_KEY_ID = OSSClientConstants.ACCESS_KEY_ID;
ACCESS_KEY_SECRET = OSSClientConstants.ACCESS_KEY_SECRET;
BACKET_NAME = OSSClientConstants.BACKET_NAME;
FORMAT = OSSClientConstants.FORMAT;
FORMATS = OSSClientConstants.FORMATS;
}
/**
* 获取阿里云OSS客户端对象
*
* @return ossClient
*/
public static OSSClient getOSSClient() {
return new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);
}
/**
* 创建存储空间
*
* @param ossClient OSS连接
* @param bucketName 存储空间
* @return
*/
public static String createBucketName(OSSClient ossClient, String bucketName) {
// 存储空间
final String bucketNames = bucketName;
if (!ossClient.doesBucketExist(bucketName)) {
// 创建存储空间
Bucket bucket = ossClient.createBucket(bucketName);
logger.info("创建存储空间成功");
return bucket.getName();
}
return bucketNames;
}
/**
* 删除存储空间buckName
*
* @param ossClient oss对象
* @param bucketName 存储空间
*/
public static void deleteBucket(OSSClient ossClient, String bucketName) {
ossClient.deleteBucket(bucketName);
logger.info("删除" + bucketName + "Bucket成功");
}
/**
* 创建模拟文件夹
*
* @param ossClient oss连接
* @param bucketName 存储空间
* @param folder 模拟文件夹名如"qj_nanjing/"
* @return 文件夹名
*/
public static String createFolder(OSSClient ossClient, String bucketName, String folder) {
// 文件夹名
final String keySuffixWithSlash = folder;
// 判断文件夹是否存在,不存在则创建
if (!ossClient.doesObjectExist(bucketName, keySuffixWithSlash)) {
// 创建文件夹
ossClient.putObject(bucketName, keySuffixWithSlash, new ByteArrayInputStream(new byte[0]));
logger.info("创建文件夹成功");
// 得到文件夹名
OSSObject object = ossClient.getObject(bucketName, keySuffixWithSlash);
String fileDir = object.getKey();
return fileDir;
}
return keySuffixWithSlash;
}
/**
* 根据key删除OSS服务器上的文件
*
* @param ossClient oss连接
* @param bucketName 存储空间
* @param key Bucket下的文件的路径名+文件名 如:"upload/cake.jpg"
*/
public static void deleteFile(OSSClient ossClient, String bucketName, String key) {
ossClient.deleteObject(bucketName, key);
logger.info("删除" + bucketName + "下的文件" + key + "成功");
}
/**
* 上传图片至OSS 文件流
*
* @param ossClient oss连接
* @param file 上传文件(文件全路径如:D:\\image\\cake.jpg)
* @param bucketName 存储空间
*
* 模拟文件夹名
* @return String 返回的唯一MD5数字签名
*/
public static String uploadObject2OSS(OSSClient ossClient, MultipartFile file, String bucketName, String folder) {
String resultStr = null;
String[] fo = new String[]{"", ""};
String fileName = "";
try {
// 以输入流的形式上传文件
// InputStream is = new FileInputStream(file);
InputStream is = file.getInputStream();
// String is = upload.getOriginalFilename();
// 文件名
String timefile = System.currentTimeMillis() + getRandomString(5);
// String fileName = file.getName();
fileName = file.getOriginalFilename();
fileName = timefile + fileName.substring(fileName.lastIndexOf("."));
logger.info("上传到路径" + folder + fileName);
// 文件大小
// Long fileSize = file.length();
Long fileSize = file.getSize();
// 创建上传Object的Metadata
ObjectMetadata metadata = new ObjectMetadata();
// 上传的文件的长度
metadata.setContentLength(is.available());
// 指定该Object被下载时的网页的缓存行为
metadata.setCacheControl("no-cache");
// 指定该Object下设置Header
metadata.setHeader("Pragma", "no-cache");
// 指定该Object被下载时的内容编码格式
metadata.setContentEncoding("utf-8");
// 文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,
// 如果没有扩展名则填默认值application/octet-stream
metadata.setContentType(getContentType(fileName));
// 指定该Object被下载时的名称(指示MINME用户代理如何显示附加的文件,打开或下载,及文件名称)
metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte.");
// 上传文件 (上传文件流的形式)
PutObjectResult putResult = ossClient.putObject(bucketName, folder + fileName, is, metadata);
// 解析结果
resultStr = putResult.getETag();
fo[1] = folder + fileName;
fo[0] = resultStr;
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
logger.error("上传阿里云OSS服务器异常." + e.getMessage(), e);
}
return OSSClientConstants.IMG_URL + folder + fileName;
}
//length用户要求产生字符串的长度
private static String getRandomString(int length){
String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Random random=new Random();
StringBuffer sb=new StringBuffer();
for(int i=0;i<length;i++){
int number=random.nextInt(62);
sb.append(str.charAt(number));
}
return sb.toString();
}
//上传路径返回地址,图片视频都可以
//需要改变上传那文件位置FOLDER
public static String[] uploadObjectOSS(OSSClient ossClient, String file, String bucketName, String user_id) {
String resultStr = null;
String[] fo = new String[]{"", ""};
try {
// 以输入流的形式上传文件
String folder = "";
folder = VIOLATION + user_id + "/" + FORMAT + "/";
// InputStream is = new FileInputStream(file);
// 文件名
String timefile = FORMATS;
// String fileName = file.getName();
file = timefile + file.substring(file.lastIndexOf("."));
logger.info("上传到路径" + folder + file);
// 文件大小
Integer fileSize = file.length();
// 创建上传Object的Metadata
ObjectMetadata metadata = new ObjectMetadata();
// // 上传的文件的长度
// metadata.setContentLength(is.available());
// 指定该Object被下载时的网页的缓存行为
metadata.setCacheControl("no-cache");
// 指定该Object下设置Header
metadata.setHeader("Pragma", "no-cache");
// 指定该Object被下载时的内容编码格式
metadata.setContentEncoding("utf-8");
// 文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,
// 如果没有扩展名则填默认值application/octet-stream
metadata.setContentType(getContentType(file));
// 指定该Object被下载时的名称(指示MINME用户代理如何显示附加的文件,打开或下载,及文件名称)
metadata.setContentDisposition("filename/filesize=" + file + "/" + fileSize + "Byte.");
// 上传文件 (上传文件流的形式)
PutObjectResult putResult = ossClient.putObject(bucketName, folder + file, new ByteArrayInputStream(file.getBytes("UTF-8")), metadata);
// 解析结果
resultStr = putResult.getETag();
fo[1] = folder + file;
fo[0] = resultStr;
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
logger.error("上传阿里云OSS服务器异常." + e.getMessage(), e);
}
return fo;
}
//上传视频
public static String uploadByteVideoOSS(OSSClient ossClient, byte[] b, String bucketName, String user_id) {
// byte[] content = "Hello OSS".getBytes();
// 以输入流的形式上传文件
String folder = "";
folder = FOLDER_VIDEO + user_id + "/" + FORMAT + "/";
// 文件名
String timefile = FORMATS;// 文件名
String fileName = ".MP4";// 后缀扩展名
fileName = timefile + fileName;
logger.info("上传到路径" + folder + fileName);
Long fileSize = (long) b.length;
// 创建上传Object的Metadata
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(fileSize);
// 指定该Object被下载时的网页的缓存行为
metadata.setCacheControl("no-cache");
// 指定该Object下设置Header
metadata.setHeader("Pragma", "no-cache");
// 指定该Object被下载时的内容编码格式
metadata.setContentEncoding("utf-8");
// 文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,
// 如果没有扩展名则填默认值application/octet-stream
metadata.setContentType(getContentType(fileName));
// 指定该Object被下载时的名称(指示MINME用户代理如何显示附加的文件,打开或下载,及文件名称)
metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte.");
PutObjectResult putResult = ossClient.putObject(bucketName, folder + fileName, new ByteArrayInputStream(b),
metadata);
ossClient.shutdown();
String filepath = folder + fileName;
return filepath;
}
//上传图片
public static String uploadByteOSS(OSSClient ossClient, byte[] b, String bucketName, String user_id) {
// byte[] content = "Hello OSS".getBytes();
// 以输入流的形式上传文件
String folder = "";
folder = VIOLATION + user_id + "/" + FORMAT + "/";
// 文件名
String timefile = FORMATS;// 文件名
String fileName = ".jpg";// 后缀扩展名
fileName = timefile + fileName;
logger.info("上传到路径" + folder + fileName);
Long fileSize = (long) b.length;
// String timefile = FORMATS;
String fileName = file.getName();
// file = timefile + file.substring(file.lastIndexOf("."));
// 创建上传Object的Metadata
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(fileSize);
// 指定该Object被下载时的网页的缓存行为
metadata.setCacheControl("no-cache");
// 指定该Object下设置Header
metadata.setHeader("Pragma", "no-cache");
// 指定该Object被下载时的内容编码格式
metadata.setContentEncoding("utf-8");
// 文件的MIME,定义文件的类型及网页编码,决定浏览器将以什么形式、什么编码读取文件。如果用户没有指定则根据Key或文件名的扩展名生成,
// 如果没有扩展名则填默认值application/octet-stream
metadata.setContentType(getContentType(fileName));
// 指定该Object被下载时的名称(指示MINME用户代理如何显示附加的文件,打开或下载,及文件名称)
metadata.setContentDisposition("filename/filesize=" + fileName + "/" + fileSize + "Byte.");
PutObjectResult putResult = ossClient.putObject(bucketName, folder + fileName, new ByteArrayInputStream(b),
metadata);
ossClient.shutdown();
String filepath = folder + fileName;
return filepath;
}
public static byte[] image2Bytes(String imgSrc) throws Exception {
FileInputStream fin = new FileInputStream(new File(imgSrc));
// 可能溢出,简单起见就不考虑太多,如果太大就要另外想办法,比如一次传入固定长度byte[]
byte[] bytes = new byte[fin.available()];
// 将文件内容写入字节数组,提供测试的case
fin.read(bytes);
fin.close();
return bytes;
}
//图片转化为byte数组
public static byte[] image2byte(String path) {
byte[] data = null;
FileImageInputStream input = null;
try {
input = new FileImageInputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = input.read(buf)) != -1) {
output.write(buf, 0, numBytesRead);
}
data = output.toByteArray();
output.close();
input.close();
} catch (FileNotFoundException ex1) {
ex1.printStackTrace();
} catch (IOException ex1) {
ex1.printStackTrace();
}
return data;
}
/**
* 通过文件名判断并获取OSS服务文件上传时文件的contentType
*
* @param fileName 文件名
* @return 文件的contentType
*/
public static String getContentType(String fileName) {
// 文件的后缀名
String fileExtension = fileName.substring(fileName.lastIndexOf("."));
if (".bmp".equalsIgnoreCase(fileExtension)) {
return "image/bmp";
}
if (".gif".equalsIgnoreCase(fileExtension)) {
return "image/gif";
}
if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension)
|| ".png".equalsIgnoreCase(fileExtension)) {
return "image/jpeg";
}
if (".html".equalsIgnoreCase(fileExtension)) {
return "text/html";
}
if (".txt".equalsIgnoreCase(fileExtension)) {
return "text/plain";
}
if (".vsd".equalsIgnoreCase(fileExtension)) {
return "application/vnd.visio";
}
if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
return "application/vnd.ms-powerpoint";
}
if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
return "application/msword";
}
if (".xml".equalsIgnoreCase(fileExtension)) {
return "text/xml";
}
if (".mp4".equalsIgnoreCase(fileExtension)) {
return "video/mp4";
}
// 默认返回类型
return "image/jpeg";
}
/**
* 获得url链接
*
* @return
*/
public static String getUrl(OSSClient ossClient, String bucketName, String fileName) {
// 设置URL过期时间为10年 3600l* 1000*24*365*10
Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 10);
// 生成URL
URL url = ossClient.generatePresignedUrl(bucketName, fileName, expiration);
if (url != null) {
return url.toString();
}
return "获网址路径出错";
}
/**
* 判断文件大小
*
* @param len
* 文件长度
* @param size
* 限制大小
* @param unit
* 限制单位(B,K,M,G)
* @return
*/
public static boolean checkFileSize(Long len, int size, String unit) {
// long len = file.length();
double fileSize = 0;
if ("B".equals(unit.toUpperCase())) {
fileSize = (double) len;
} else if ("K".equals(unit.toUpperCase())) {
fileSize = (double) len / 1024;
} else if ("M".equals(unit.toUpperCase())) {
fileSize = (double) len / 1048576;
} else if ("G".equals(unit.toUpperCase())) {
fileSize = (double) len / 1073741824;
}
if (fileSize > size) {
return false;
}
return true;
}
/**
* 根据url删除
* @param url
*/
public static void deteleFromURL(String url){
String str=url.substring(0,url.lastIndexOf("/"));
String detUrl=url.substring(str.lastIndexOf("/")+1);
deleteFile(getOSSClient(),OSSClientConstants.BACKET_NAME,detUrl);
}
}
4.OSSClientConstants
OSS配置常量类
package com.mla.util;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* @author lts
* @date
*/
public class OSSClientConstants {
//图片服务器外网访问url
public static final String IMG_URL = "xxxxx";
//阿里云API的外网域名
public static final String ENDPOINT = "xxxxx";
//阿里云API的密钥Access Key ID
public static final String ACCESS_KEY_ID = "xxxxx";
//阿里云API的密钥Access Key Secret
public static final String ACCESS_KEY_SECRET = "xxxxx";
//阿里云API的bucket名称
public static final String BACKET_NAME = "xxxxx";
//阿里云API的文件夹名称
public static final String PICLOCATION="mlacs/";
public static final String FORMAT = new SimpleDateFormat("yyyyMMdd").format(new Date());
public static final String FORMATS = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
//图片服务器外网访问url
public static final String PARTURL = "xxxxx";
}
5.前端代码
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="http://libs.baidu.com/jquery/2.1.4/jquery.min.js">script>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
head>
<body>
<div id="uploader">
<div class="btns">
<input id="file" name="file" type="file"/>
<br>
<br>
<button id="startBtn">
开始上传
button>
br>
br>
div>
<div id="output">
div>
<div id="pathurl">
div>
body>
<script type="text/javascript">
var status = 0;
var page = {
init: function(){
$("#startBtn").click($.proxy(this.upload, this));
},
upload: function(){
status = 0;
var tthis = this;
var file = $("#file")[0].files[0], //文件对象
name = file.name, //文件名
size = file.size; //总大小
var shardSize = 1 * 1024 * 1024, //以1MB为一个分片
shardCount = Math.ceil(size / shardSize); //总片数
$.ajax({
url:"http://localhost:8080/part/ossPartReady",
type:"POST",
data:{fileTypeName:name.substring(name.indexOf(".")+1)},
success:function(res){
if(res.code != 200){
$("#output").html(res.msg);
return;
}
var data = res.obj;
for(var i = 0;i < shardCount;++i){
//计算每一片的起始与结束位置
var start = i * shardSize,
end = Math.min(size, start + shardSize);
var partFile = file.slice(start,end);
tthis.partUpload(data.objectKey,partFile,i+1,data.uploadId,shardCount,data.tagId);
}
}
})
},
partUpload:function(objectKey,partFile,partNumber,uploadId,partCount,tagId){
//构造一个表单,FormData是HTML5新增的
var now = this;
var form = new FormData();
form.append("objectKey", objectKey); //上传key
form.append("file", partFile); //slice方法用于切出文件的一部分
form.append("partNumber", partNumber); //当前是第几片
form.append("uploadId", uploadId); //上传id
form.append("partCount", partCount); //总片数
form.append("tagId",tagId); //组装id
//Ajax提交
$.ajax({
url: "http://localhost:8080/part/ossPartUpload",
type: "POST",
data: form,
async: true, //异步
processData: false, //很重要,告诉jquery不要对form进行处理
contentType: false, //很重要,指定为false才能形成正确的Content-Type
success: function(data){
if(data.code == 200){
$("#output").html(data.obj);//返回访问路径
return;
}
console.log("上传失败");
}
});
},
mergeFile:function(GUID,name){
var formMerge = new FormData();
formMerge.append("guid", GUID);
formMerge.append("fileName", name);
$.ajax({
url: "http://localhost:12788/part/merge",
type: "POST",
data: formMerge,
processData: false, //很重要,告诉jquery不要对form进行处理
contentType: false, //很重要,指定为false才能形成正确的Content-Type
success: function(data){
if(data.code == 200){
$("#pathurl").text(data.obj);
}
}
});
},
guid:function(prefix){
var counter = 0;
var guid = (+new Date()).toString( 32 ),
i = 0;
for ( ; i < 5; i++ ) {
guid += Math.floor( Math.random() * 65535 ).toString( 32 );
}
return (prefix || 'wu_') + guid + (counter++).toString( 32 );
}
};
$(function(){
page.init();
});
script>
html>