图片上传类
package cn.wizzer.modules.controllers.open.file;
import cn.wizzer.common.base.Globals;
import cn.wizzer.common.base.Result;
import cn.wizzer.common.base.ResultApp;
import cn.wizzer.common.qiniu.QiniuConfig;
import cn.wizzer.common.qiniu.UpLoadUtil;
import cn.wizzer.common.util.DateUtil;
import com.qiniu.util.StringMap;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.json.Json;
import org.nutz.lang.Files;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.*;
import org.nutz.mvc.impl.AdaptorErrorContext;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
/**
* Created by Wizzer on 2016/7/5.
*/
@IocBean
@At("/open/file/upload")
public class UploadController {
private static final Log log = Logs.get();
@AdaptBy(type = UploadAdaptor.class, args = {"ioc:imageUpload"})
@POST
@At
@Ok("json")
@RequiresAuthentication
//AdaptorErrorContext必须是最后一个参数
public Object image(@Param("Filedata") TempFile tf, HttpServletRequest req, AdaptorErrorContext err) {
try {
if (err != null && err.getAdaptorErr() != null) {
return NutMap.NEW().addv("code", 1).addv("msg", "文件不合法");
} else if (tf == null) {
return Result.error("空文件");
} else {
// String p = Globals.AppRoot;
// String f = Globals.AppUploadPath + "/image/" + DateUtil.format(new Date(), "yyyyMMdd") + "/" + R.UU32() + tf.getSubmittedFileName().substring(tf.getSubmittedFileName().indexOf("."));
// Files.write(new File(p + f), tf.getInputStream());
return Result.success("上传成功", UpLoadUtil.upLoadFile(tf));
}
} catch (Exception e) {
return Result.error("系统错误");
} catch (Throwable e) {
return Result.error("图片格式错误");
}
}
@Ok("json:full")
@At
public Object uptoken() {
String token = QiniuConfig.dummyAuth.uploadToken(QiniuConfig.dummyBucket);
return ResultApp.success("请求成功",new NutMap("uptoken", token).addv("url",QiniuConfig.dummyUrl)) ;
}
}
HTML ajaxform 提交代码如下
$('#file_upload').uploadifive({
'auto': true,
'multi': false,
'width': '100%',
'height': '35',
'buttonText': '请选择图片',
'fileType': 'image/jpg,image/jpeg,image/png',
'fileSizeLimit': 1024,
'queueSizeLimit': 1,
'formData': {},
'queueID': 'queue',
'uploadScript': '${base}/open/file/upload/image',
'onUploadComplete': function (file, data) {
// console.log(data);
data = JSON.parse(data);
if (data.code == 0) {
Toast.success(data.msg);
$("#img").html("<img src='" + data.data + "' style='width:150px;height:95px;'>");
$("#picurl").val(data.data);
} else {
clearFile();
Toast.error(data.msg);
}
},
'onDrop': function (file, fileDropCount) {
clearFile();
},
'onClearQueue': function (queue) {
clearFile();
},
'onCancel': function () {
clearFile();
}
});
$("#save").on("click", function () {
$("#footer_content").val(ue.getContent());
$('#editForm').submit();
});
$('#editForm').ajaxForm({
dataType: 'json',
contentType: "application/json",
beforeSubmit: function (arr, form, options) {
form.find("button:submit").button("loading");
},
success: function (data, statusText, xhr, form) {
if (data.code == 0) {
Toast.success(data.msg);
// setTimeout(function () {
// $("#goback").trigger("click");
// }, 1000);
} else {
Toast.error(data.msg);
}
form.find("button:submit").button("reset");
}
});
我用 ajaxform 上传文件
正常表单提交
Content-Type: application/x-www-form-urlencoded
上传文件后 莫名其妙 变成
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryGKXjbv1WtEeg0WRI
@param("..") 获取不到传的值
有办法解决吗