[Exception Shoot]-NullPointerException when uploading a file(struts文件上传)
来源:互联网 发布:淘宝网现状分析 编辑:程序博客网 时间:2024/05/16 15:12
环境:SSH
要实现功能:在工程添加一个文件上传功能,,已经做了中文字符处理、文件大小处理。文件上传时出现了 NullPointerException 。
- Action代码:
private File file;private String contentType;private String fileName; public String fileUpload() throws Exception{ File saveFile = new File("/Users/Zus/Desktop/securitymgr/WebRoot/exportFiles/" + fileName); String status =null; if (!saveFile.getParentFile().exists()){ saveFile.getParentFile().mkdirs(); status="成功的边缘"; } FileUtils.copyFile(file, saveFile);// 复制文件 this.addFieldError("isSuccess", "文件上传成功!"); servletHandler.writeToClient(ServletActionContext.getResponse(), status); return SUCCESS;}
- Action代码:
2.struts-file.xml :
<constant name="struts.i18n.encoding" value="GBK" /> <constant name="struts.multipart.maxSize" value="3145728" /><constant name="struts.multipart.saveDir" value="/Users/Zus/Desktop/securitymgr/WebRoot/exportFiles" /><package name="test" extends="json-default" namespace="/test"> <default-interceptor-ref name="defaultStack"/> <action name="test_*" class="action.test.TestAction" method="{1}"> <result type="json" /> </action> <!--<action name="test*" class="action.test.TestAction" method="{1}">--> <!--<result type="file"></result>--> <!--</action>--></package>
3. jsp页面:
<span style="white-space:pre"></span><pre name="code" class="html"> <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <title>文件上传</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"></head><body><form action="${pageContext.request.contextPath}/test/test_fileUpload.action" enctype="multipart/form-data" method="post"> 文件:<input type="file" name="file"> <input type="submit" value="上传" /></form><br/><s:fielderror /></body><span style="font-family: 'Helvetica Neue'; font-size: 12px;"></html></span>
4.上传文件的时候出现了 :
Struts Problem Report Struts has detected an unhandled exception: Messages: File: java/io/File.java Line number: 317
Stacktraces java.lang.NullPointerException java.io.File.(File.java:317)
example.uploadFile.execute(uploadFile.java:36)
StackOverFlow:可能是拦截器问题: http://stackoverflow.com/questions/29210254/nullpointerexception-when-uploading-a-file
修改后还是出错,发现不是这个问题》》》
看了以前的工程后发现命名有规则,尝试了一下竟然通过了;
jsp中如果<input type=“file” name=“anyThing”>
那么在Action中必须为:
private StringanyThingContentType;
private StringanyThingFileName;
到现在还是不知道这是什么原因;望各位在评论区留下你的观点或者m我 **whoszus#yahoo.com**
0 0
- [Exception Shoot]-NullPointerException when uploading a file(struts文件上传)
- 集群环境下文件上传方法与运维(Uploading a File to a Service)
- IE 上传文件包含绝对路径 Include local directory path when uploading files
- File uploading
- ASPxUploadControl Uploading-Large-Files 上传大文件
- ASP.NET File Uploading
- Struts(11)文件上传
- Struts(11)文件上传
- struts(八)文件上传
- Caught exception while loading file struts-default
- 如何点击a标签, 弹出input file 上传文件对话框
- send parameters while uploading file
- spring-guide之uploading-file
- HDFS API文件上传报错 Exception in thread "main" .......: Wrong FS: file:///
- Building a Gmail Style File Uploading System using ASP.NET 2.0
- NullPointerException when calling getReadableDatabase()
- struts多文件上传(1)
- Struts的文件上传
- Mac下顯示當前文件目錄(配置open cv on ios)
- 金万维怎样用
- STM32使用半主机调试
- IOS6.0自带下拉刷新控件UIRefreshControl
- TableView viewForHeaderInSection 随滚动条滚动 无遮挡
- [Exception Shoot]-NullPointerException when uploading a file(struts文件上传)
- 面试分享(四)-------浙江乾冠信息安全研究院--Android开发
- chrome使用技巧(看了定不让你失望)
- 黑马程序员---初识反射,Class,Constructor,Field
- mac虚拟机突然就不能上网了伤心
- log4cplus 在VS2010安装说明
- OK6410之tftp下载内核,nfs挂载文件系统全过程详解
- 使用代码为 Xcode 工程添加文件
- 朴素贝叶斯