upload.jsp
<input name = "file " type="file"/>
pom.xml
<!-- 阿里云oss 获取相应的包 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.0.1</version>
</dependency>
uploadUtil.java
//文件,文件名,需要上传的文件夹路径(国际化配置,http://image.yunpeilian.cn)
//获取上传后的图片路径
public static String createUrl(File file,String fileName,String showUrl){
String date = new SimpleDateFormat("yyMMdd").format(new Date());
String bucketName = "yplimg";
String key = "user/" + date + "/" + createName() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
Date date2 = new Date();
OSSClient client = new OSSClient("http://oss-cn-beijing-internal.aliyuncs.com", "t48OaCMFBfSrOS5K", "Pd3I1BjNNwAXCTM2baY6SyPhzQ9YKb");
try {
ObjectMetadata objectMeta = new ObjectMetadata();
objectMeta.setContentLength(file.length());
// 可以在metadata中标记文件类型
objectMeta.setContentType("image/jpeg");
objectMeta.setCacheControl("max-age=8640000");
InputStream input = new FileInputStream(file);
PutObjectResult r = client.putObject(bucketName, key, input, objectMeta);
logger.debug("upload img oss ETag={}", r.getETag());
} catch(Exception ex) {
logger.error("upload img oss time = {}", ex);
}
Date date3 = new Date();
logger.debug("upload img oss time = {}", date3.getTime() - date2.getTime());
String imageUrl = showUrl + "/" + key;
logger.debug("UploadFileUtil imageUrl = {}" + imageUrl);
return imageUrl;
}
upload.java
public class upload{
private File file;
private String fileFileName; //文件名称
private String fileContentType; //文件类型
public static void upload(){
system.out.print("URL:" + uploadUtil.createUrl(file,fileFileName,"http://image.yunpeilian.cn"));
}
}