NutzCN Logo
问答 图片上传报错,不知道是哪里出的问题了
发布于 2057天前 作者 qq_91afbf68 1301 次浏览 复制 上一个帖子 下一个帖子
标签:

使用图片上传的时候,报的这个错误

[DEBUG] 2019-03-29 17:12:34,653 org.nutz.mvc.impl.UrlMappingImpl.get(UrlMappingImpl.java:101) - Found mapping for [POST] path=/open/file/upload/upImg : UploadController.upImg(UploadController.java:90)
[DEBUG] 2019-03-29 17:12:34,654 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'permission'<class cn.wizzer.common.shiro.view.Permission>
[DEBUG] 2019-03-29 17:12:34,654 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'dateUtil'<class cn.wizzer.common.util.DateUtil>
[DEBUG] 2019-03-29 17:12:34,654 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'stringUtil'<class cn.wizzer.common.util.StringUtil>
[DEBUG] 2019-03-29 17:12:34,654 org.nutz.ioc.impl.NutIoc.get(NutIoc.java:151) - Get 'uploadController'<class cn.wizzer.modules.controllers.open.file.UploadController>
[DEBUG] 2019-03-29 17:12:34,662 cn.wizzer.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:26) - [POST]URI=/bbnutzwk/open/file/upload/upImg 8ms
[WARN ] 2019-03-29 17:12:34,663 org.nutz.mvc.impl.processor.FailProcessor.process(FailProcessor.java:28) - Error@/open/file/upload/upImg :
java.lang.ClassCastException: org.apache.shiro.web.servlet.ShiroHttpServletRequest cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest
	at cn.wizzer.modules.controllers.open.file.UploadController$FM$upImg$397f5b1125a5052a5d53f338a478385c.invoke(UploadController.java)
	at org.nutz.mvc.impl.processor.MethodInvokeProcessor.process(MethodInvokeProcessor.java:31)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.AdaptorProcessor.process(AdaptorProcessor.java:33)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ActionFiltersProcessor.process(ActionFiltersProcessor.java:58)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.common.processor.XssSqlFilterProcessor.process(XssSqlFilterProcessor.java:35)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.common.processor.NutShiroProcessor.process(NutShiroProcessor.java:52)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.ModuleProcessor.process(ModuleProcessor.java:123)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.EncodingProcessor.process(EncodingProcessor.java:27)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at org.nutz.mvc.impl.processor.UpdateRequestAttributesProcessor.process(UpdateRequestAttributesProcessor.java:15)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.common.processor.GlobalsSettingProcessor.process(GlobalsSettingProcessor.java:37)
	at org.nutz.mvc.impl.processor.AbstractProcessor.doNext(AbstractProcessor.java:44)
	at cn.wizzer.common.processor.LogTimeProcessor.process(LogTimeProcessor.java:21)
	at org.nutz.mvc.impl.NutActionChain.doChain(NutActionChain.java:44)
	at org.nutz.mvc.impl.ActionInvoker.invoke(ActionInvoker.java:67)
	at org.nutz.mvc.ActionHandler.handle(ActionHandler.java:31)
	at org.nutz.mvc.NutFilter.doFilter(NutFilter.java:202)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at cn.wizzer.common.filter.RouteFilter.doFilter(RouteFilter.java:37)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61)
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108)
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
	at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800)
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:806)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1498)
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
	at java.lang.Thread.run(Thread.java:748)
[DEBUG] 2019-03-29 17:12:36,708 org.quartz.impl.jdbcjobstore.JobStoreSupport$MisfireHandler.manage(JobStoreSupport.java:3949) - MisfireHandler: scanning for misfires...
[DEBUG] 2019-03-29 17:12:36,846 org.quartz.impl.jdbcjobstore.JobStoreSupport.doRecoverMisfires(JobStoreSupport.java:3198) - Found 0 triggers that missed their scheduled fire-time.
[DEBUG] 2019-03-29 17:12:44,920 org.quartz.impl.jdbcjobstore.JobStoreSupport$ClusterManager.manage(JobStoreSupport.java:3877) - ClusterManager: Check-in complete.

9 回复

引用这种类?你是认真的吗?

org.springframework.web.multipart.MultipartHttpServletRequest

TempFile 使用这个的话,会让我获取到的不能转MultipartFile

nutz没有MultipartFile这个类

嗯嗯 所以我使用到TempFile 是能够获取到,但是我不知道怎么在里面java七牛云上传了,不是文档中的那种js上传的。

你想要的是TempFile获得File实例吧?.getFile方法就行啦

我看到这个文档是这种方式,是可以用获取@Param("..")NutMap nm来接收,也能ServletContext 对象注入 MyUtils
https://blog.csdn.net/mamacmm/article/details/83914479

ioc文件夹下,添加一个名称为 myUpload.js 的文件(名称无所谓,随便起),里面的内容如下:

 

var myUploadIoc = {
	utils : {     
		type : 'com.mm.util.MyUtils',     
		fields : {         
			sc : {app:'$servlet'}   // 将 ServletContext 对象注入 MyUtils
		}
	},
	tmpFilePool : {     
		type : 'org.nutz.filepool.NutFilePool',     // 临时文件最大个数为 1000 个     
		args : [ {java:'$utils.getPath("/tmp")'}, 1000 ]    
	},
	uploadPicFileContext : { 
		type : 'org.nutz.mvc.upload.UploadingContext',     
		singleton : false,     
		args : [ { refer : 'tmpFilePool' } ],
		fields : {
			// 是否忽略空文件, 默认为 false         
			ignoreNull : true, 
			// 单个文件最大尺寸(大约的值,单位为字节,即 1048576 为 1M)         
			maxFileSize : 1048576,         // 正则表达式匹配可以支持的文件名         
			nameFilter : '^(.+[.])(gif|jpg|png)$'
		}
	},
	myPicUpload : {     
		type : 'org.nutz.mvc.upload.UploadAdaptor',     
		singleton : false,     
		args : [ { refer : 'uploadPicFileContext' } ]  
	}
};
 

注意:

 

com.mm.util.MyUtils 是自己定义的一个工具类,为了得到上传地址,内容如下:

 

package com.mm.util;
 
import javax.servlet.ServletContext;
 
/**
 * 用于上传ioc : myUpload.js 
 *
 */
public class MyUtils {
	private ServletContext sc;      
	public String getPath(String path) {         
		return sc.getRealPath(path);     
	} 
}
 

tmpFilePool 是Nutz的文件池;

java:'$utils.getPath("/tmp")'  这个是指明了上传的图片的地址是:项目根目录下的tmp目录下;

gif|jpg|png  限制了上传文件的格式。
/**
 * 添加照片
* @param albumId 相册ID
* @param req 
*/
@At("/photo/pho/addPhoto")
@Ok("fm:/common/execute_result.ftl")
@AdaptBy(type = UploadAdaptor.class, args = { "ioc:myPicUpload" })
public void addPhoto(@Param("..")NutMap nm,HttpServletRequest req){
	//log.info("NutMap:"+nm);
	TempFile tf = (TempFile)nm.get("photoFile");
	File f = tf.getFile();
	//FieldMeta meta = tf.getMeta();
	//String oldName = meta.getFileLocalName();
	//log.info("文件绝对路径:"+f.getAbsolutePath()+",原来的名称:"+oldName);
		
	String str = f.getPath();
	int one = str.indexOf(Constants.UPLOAD_FILE_DIRECTORY_NAME);
	str = str.substring(one);
	//log.info("文件的相对路径:"+str);
 
	long albumId = (long)nm.getInt("albumId");
	String photoName = (String)nm.get("photoName");
	String description = (String)nm.get("description");
	Photo photo = new Photo();
	photo.setAlbumId(albumId);
	photo.setPhotoName(photoName);
	photo.setDescription(description);
	photo.setCreateDate(TimeUtil.getNowTime("yyyy-MM-dd"));
//	photo.setPhotoUrl(f.getAbsolutePath());
	photo.setPhotoUrl(str);
	getServiceManager().getPhotoService().insert(photo);
		
	setExecuteResult(null);
	addMessage("添加照片成功!");
	addRedirURL("返回", "@back");
	addRedirURL("照片列表", "photoList.html?albumId="+String.valueOf(albumId));
	req.setAttribute(EXECUTE_RESULT, getExecuteResult());
}
 

可以那么玩?

感谢大佬,之前没有认真看七牛云文档

添加回复
请先登陆
回到顶部