首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

帮小弟看个struts2上传文件报错信息,

2012-01-05 
帮小弟看个struts2上传文件报错信息,,在线等!!!前台jsp:form action%basePath%m/file/uploadFile.ac

帮小弟看个struts2上传文件报错信息,,在线等!!!
前台jsp:
  <form action="<%=basePath%>m/file/uploadFile.action" method="post" enctype="multipart/form-data">  
  <p><input type="text" name="fileName" id="fileName" value=""></input>File Description</p>  
  <p><input type="file" name="upload" id="upload" value="file"></input></p>  
  <p><input type="submit" value="UpLoad"></input></p>  
  </form>

struts配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="utf-8"/>
<constant name="struts.multipart.maxSize" value="-1"/>
<constant name="struts.multipart.saveDir" value="/tmp"/>
<package name="file" namespace="/m/file" extends="json-default">
<global-results>
<result type="json">
<param name="ignoreHierarchy">false</param>
<!-- 输出父类属性 -->
<param name="excludeNullProperties">true</param>
<!-- 排除空属性 -->
</result>
<result name="error" type="json">
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">msg,msg.code,msg.text</param>
</result>
</global-results>
<action name="uploadFile" class="fileUpLoadAction" method="fileUpload">
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/bmp, image/tmp, image/xml, image/x-png, image/gif, image/jpeg</param>
<param name="maximumSize">204800</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="input">/index.jsp</result>
<param name="savePath">/upload</param>
<result>/page/UpLoad.jsp</result>
</action>
</package>
</struts>
后台action:
package com.start.action;

import java.io.*;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.start.upload.FileFiler;

public class FileUpLoadAction extends ActionSupport {
//private FileFiler file;
//
//public FileFiler getFile() {
//return file;
//}
//
//public void setFile(FileFiler file) {
//this.file = file;
//}
private String fileName;
private File upload;
private String uploadContentType;
private String uploadFileName; 
private String savePath;

public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {


this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);

}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public String fileUpload() throws Exception{

//以服务器的文件保存地址和原文件名建立上传文件输出流

FileOutputStream fos=new FileOutputStream(this.getSavePath()+"\\"+this.getUploadFileName());
FileInputStream fis=new FileInputStream(this.getUpload());
byte[] buffer=new byte[1024];
int len=0;
while((len=fis.read(buffer))>0){
fos.write(buffer,0,len);
}

return SUCCESS;
}
}
 
情况是这样的,debug跟踪到后台FileInputStream fis=new FileInputStream(this.getUpload());再往下就是报错了,文件可以上传,后台也没有报错,但是前台报错了,说临时文件找不到。大大们帮下小弟吧!!

[解决办法]
java.io.FileNotFoundException: \tmp\upload__3e019d96_12ec6707d88__7fff_00000001.tmp (系统找不到指定的文件。)

相对路径不行 就换成绝对路径试试
[解决办法]

Java code
FileInputStream fis = new FileInputStream("这里应当是文件的路径和文件名字") 

热点排行