我用struts2文件作文件上传碰到在局域网和本机文件都能上传,但是到了外网上,有些文件就不能上传。
java类public abstract class AbstractIOAction extends AbstractAction { private static final long serialVersionUID = -6516966167635959378L;
private File uploadFile;// 实际上传文件
private String uploadFileContentType; // 文件的内容类型
private String uploadFileFileName; // 上传文件名
private String fileType;// 上传文件选择类型 0:CSV, 1:DBF, 2:excel public File getUploadFile() {
return uploadFile;
} public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
} public String getFileType() {
return fileType;
} public void setFileType(String fileType) {
this.fileType = fileType;
} public String getUploadFileContentType() {
return uploadFileContentType;
} public void setUploadFileContentType(String uploadContentType) {
this.uploadFileContentType = uploadContentType;
} public String getUploadFileFileName() {
return uploadFileFileName;
} public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
} public String uploadFile() throws Exception {
if (!doFileUpload()) {
return INPUT;
}
return SUCCESS;
} /**
 * 上传文件
 * 
 * @return
 * @throws IOException
 */
protected boolean doFileUpload() throws IOException {
initPath();
String targetDirectory = ServletActionContext.getServletContext().getRealPath("/upload");
String targetFileName = uploadFileFileName.substring(0, uploadFileFileName.lastIndexOf(".")) + "_" + UserContext.getUser().getUsername() + uploadFileFileName.substring(uploadFileFileName.lastIndexOf("."));
File target = new File(targetDirectory, targetFileName);
FileUtils.copyFile(uploadFile, target);
setUploadFileFileName(targetFileName);
return true;
} /**
 * 初始化文件夹
 */
protected void initPath() {
// 文件夹不存在就自动创建:
ServletContext context = ServletActionContext.getServletContext();
if (!new File(context.getRealPath("/upload")).isDirectory()) {
new File(context.getRealPath("/upload")).mkdirs();
}
if (!new File(context.getRealPath("/download")).isDirectory()) {
new File(context.getRealPath("/download")).mkdirs();
}
}
}推测是uploadFileFileName值没拿到
我的struts文件配置:<action name="DataImport" class="com.zksoft.web.action.data.DataImportAction">
<interceptor-ref name="fileUploadStack"> 
<param name="maximumSize">204800000</param>
</interceptor-ref>
<result name="success" type="redirect">
<param name="location">${location}</param>
</result>
 <result name="input">/major_dic_import.html</result> 
</action>
求高手指点

解决方案 »

  1.   

    lz 再 doFileUpload 方法中 try catch 一下异常看看  
      

  2.   

    后台会报一个nullpoint,空指针异常
      

  3.   

     找到你的页面的form表单看是否做了enctype="multipart/form-data"属性的设置!
      

  4.   

    错误: 
    null 
    错误内容: 
    java.lang.NullPointerException at com.zksoft.web.action.directory.AbstractIOAction.doFileUpload(AbstractIOAction.java:68) at com.zksoft.web.action.data.DataImportAction.doExecute(DataImportAction.java:36) at com.zksoft.web.action.AbstractAction.execute(AbstractAction.java:31) at sun.reflect.GeneratedMethodAccessor61.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:404) at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:267) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:229) at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:123) at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:167) at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:86) at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:223) at com.opensymphony.xwork2.util.profiling.UtilTimerStack.profile(UtilTimerStack.java:455) at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:221) at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:83) at com.opensymphony.xwork2.DefaultActionInvocation$2.doProfiling(DefaultActionInvocation.java:224) at 
      

  5.   

    我没用ajax调用过~应该不存在异步调用吧