oss上传图片

<body>
<div>
<p>店铺门图:</p>
<div class="updatebox">
<div class="update-header">
<input id="upload_imgurl" type="button" />
</div>
<div class="update-header1" style="float:left;margin-top:0px;" >
</div>
<input type="hidden" id="imgurl" name="imgurl" value=""/>
<div class="update-imgbox">
<div class="update-img" id='img_imgurl' 
style="background:url() center no-repeat;background-size:100% auto">
</div>
</div>
</div>
 <input type="hidden" id="hdBasePath" value="<%=basePath%>">
</div>
 
<div id="p" class="easyui-progressbar"  style="width:400px;"></div>
</body>


<script type="text/javascript">
$(function(){
var basePath = $("#hdBasePath").val();
upLoadSingleFile(basePath, basePath+ "/user/upload.do","", "upload_imgurl","imgurl","img_imgurl")
})

</script>

JS

function upLoadSingleFile(base_dir, my_upload_url, my_file_types, my_bound_btn,
file_url,viewId) {
if (my_file_types === "" || my_file_types === undefined) {
my_file_types = "*.jpg;*.png;*.jpeg;";
}


var flashurl = base_dir + "/js/swf/swfupload.swf"


return new SWFUpload(
{
upload_url : my_upload_url, // 后台处理路径
flash_url : flashurl, // flash加载路径
file_size_limit : "5 MB", // 文件大小限制
file_upload_limit : "30", // 允许上传文件的数量,0为无限制
file_queue_limit : "1", // 上传队列允许等待文件的数量
file_types : my_file_types, // 文件类型
file_types_description : "Image Files", // 文件类型说明
file_post_name : "filedata", // 文件名称,默认为Filedata,Linux下只能用默认名称
file_dialog_start_handler : fileDialogStart,
file_queued_handler : fileQueued,
file_queue_error_handler : fileQueueError, // 出错时执行的函数
file_dialog_complete_handler : function(numFilesSelected,
numFilesQueued) {
if (numFilesSelected > 1) {
layer.msg('您好,只能选择一个附件~');
} else {
this.startUpload();
}
},
upload_start_handler : uploadStart,
upload_error_handler : uploadError,


// 处理进度条
upload_progress_handler : function(file, bytesLoaded,
bytesTotal) {
//使用easyui设置进度条;
var value = $('#p').progressbar('getValue'); 
if (value < 100){ 
value=bytesLoaded*1.0/bytesTotal;
   $('#p').progressbar('setValue', value); 
}
},


// 清空进度条
upload_complete_handler : function(file) {
//开始上传,也就是恢复上传按钮
this.startUpload();
//进度条关闭
$.messager.progress('close');
},


// 上传成功回调函数
upload_success_handler : function(file, result, callback) {
var $fileurl = $('#' + file_url);
if ($fileurl) {
if (result != null) {
var objResult = $.parseJSON(result);
if (objResult.success) {
$("#"+viewId).css(
{
"background" : "url("
+ objResult.msg
+ ") center no-repeat",
"background-size" : "100% auto"
});
$fileurl.val(objResult.msg.substr(objResult.msg
.substr(0,
objResult.msg.lastIndexOf('/'))
.lastIndexOf('/') + 1));
} else {
alert(objResult.msg);
}
}
}
},


// 关于按钮的一些属性设置
button_placeholder_id : my_bound_btn, // 绑定按钮的id
button_text : "<span class='redText'>从本地上传图片</span>",
button_text_style : ".redText {color: #ffffff;font-size: 14px; font-weight:800;}",
button_image_url : base_dir + "/images/btn_upload_back.png",
button_width : 140,
button_height : 34,
button_text_top_padding : 8,
button_text_left_padding : 22,
button_cursor : SWFUpload.CURSOR.HAND, // 鼠标样式
button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES, // 单文件上传

});

controller

/**
* 上传附件 图片
*/
@RequestMapping("/upload")
public void upload(HttpServletRequest request,
HttpServletResponse response) throws Exception {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
WebController web = new WebController();
web.upload(multipartRequest, response, "img_folder","haimi1",
"LTAI6Lp9fhuEZXXb","zKEWaGIxcO2R2LLThLprzofmKrfHXd", "http://haimi1.oss-cn-beijing.aliyuncs.com/",
"http://oss-cn-beijing.aliyuncs.com");

}

/**
*不同尺寸压缩图片上传方法
*folder:图片存放的位置
*bucketName:阿里云oss上传服务bucketname
*access_id:阿里云oss上传服务APPID
*access_key:阿里云osss上传服务key
*resourcePath:图片和附件存放服务器地址
*endpoint:阿里云oss上传服务endpoint
*/
@RequestMapping("/upload")
public void upload(HttpServletRequest request,
HttpServletResponse response, String folder, String bucketName,
String access_id, String access_key, String resourcePath,
String endpoint) throws Exception {
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver();
if (multipartResovler.isMultipart(request)) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Iterator<String> it = multipartRequest.getFileNames();
while (it.hasNext()) {
MultipartFile file = multipartRequest.getFile((String) it
.next());
if (file != null) {
// 1、上传到服务器临时文件夹
String uploadFileName = file.getOriginalFilename();
String nfix = "";// 后缀名
if (StringUtils.isNotBlank(uploadFileName)) {
nfix = uploadFileName.substring(uploadFileName
.lastIndexOf("."));
}
String nowDate = DateUtil.getNowShortDate();// 当前时间(年月日)
String fileName = UUID.randomUUID().toString() + nfix;
String key = folder+"/"+ nowDate + "/" + fileName;// 文件名


ALiYunUtil aliyun = new ALiYunUtil(endpoint, access_id,
access_key);
if (aliyun.uploadObject(file, bucketName, key,
".ipeg,.jpg image/jpeg")) {
aliyun.shutDown();
return;
} else {
// 移动失败
aliyun.shutDown();
System.out.println("上传失败");
return;
}
}
}
}
System.out.println("上传失败");
return;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值