一、后端使用spring-web-5.2.3.RELEASE.jar的MultipartHttpServletRequest类处理文件上传
import com.aliyun.openservices.oss.OSSClient;
import com.aliyun.openservices.oss.model.ObjectMetadata;
import com.aliyun.openservices.oss.model.PutObjectResult;
import org.apache.commons.lang.RandomStringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@RestController
public class filetest {
private static String tmpPath = "D:\\file";
private static String alPath = "picture/";
@RequestMapping("/upload_original_pic")
@ResponseBody
public Map<String, Object> perFileUpload(HttpServletRequest request) {
String fileFieldStorageFileName = "";
File file = null;
Map<String, Object> response = new HashMap<>();
try {
MultipartFile requestFile = ((MultipartHttpServletRequest) request).getFile("file");
if (requestFile == null) {
response.put("errorMessage", "文件为空");
return response;
}
File dest = new File(tmpPath);
if (!dest.exists()) {
dest.mkdirs();
}
String fileName = requestFile.getOriginalFilename();
String contentType = requestFile.getContentType();
String fileExtension = com.google.common.io.Files.getFileExtension(fileName);
String alfielName = DateUtils.getDateString(new Date(), "yyyyMMddHHmmss") + RandomStringUtils.random(8, true, false);
file = new File(tmpPath + "/" + alfielName + "." + fileExtension);
requestFile.transferTo(file);
String formatName = alPath + "/" + file.getName();
fileFieldStorageFileName = putObject(formatName, file.getAbsolutePath(), contentType);
} catch (Exception e) {
response.put("errorMessage", "上传图片失败!" + e.getMessage());
return response;
} finally {
if (file != null) {
file.delete();
}
}
response.put("filePath", "上传图片成功路径为:"+fileFieldStorageFileName);
return response;
}
private static String SSO_BUCKET = "hao91-test";
private static String ALIYUN_END_POINT = "http://oss-cn-qingdao.aliyuncs.com/";
private static String accessKeyId = "LTxxxxxxxx006hNp";
private static String accessKeySecret = "YPsOWxxxxxxxxxxxxxxxqct2LQtMrl";
public String putObject(String formatName, String filePath, String contentType) throws FileNotFoundException {
OSSClient client = new OSSClient(ALIYUN_END_POINT, accessKeyId, accessKeySecret);
File file = new File(filePath);
InputStream content = new FileInputStream(file);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(file.length());
meta.setContentType(contentType);
meta.setLastModified(new Date());
PutObjectResult result = client.putObject(SSO_BUCKET, formatName, content, meta);
try {
content.close();
} catch (IOException e) {
e.printStackTrace();
}
String[] split = ALIYUN_END_POINT.split("//");
String pasth = split[0] + "//" + SSO_BUCKET + "." + split[1] + formatName;
System.out.println("上传到阿里云的地址为:"+pasth);
return pasth ;
}
}