Struts2 file 表单上传问题

jackey_option 2014-01-08 10:43:10
表单提交到Action后,通过 ActionContext.getContext().getParameters() 结果是:

(java.util.HashMap<K,V>)
{bookFile=[Ljava.io.File;@e40293,
submit=[Ljava.lang.String;@1652020,
bookFileContentType=[Ljava.lang.String;@2023f3,
bookId=[Ljava.lang.String;@1854be,
bookFileFileName=[Ljava.lang.String;@163c17d}


Action类中有以下属性:

private File bookFile;
private String bookFileContentType;
private String bookFileFileName;
private String bookId;

也有相应的setter/getter方法

@Component("bookUploadAction")
public class RBookUploadAction extends ActionSupport implements RequestAware, ServletResponseAware{
private static final long serialVersionUID = -3430678334134919673L;
private RequestMap request;
private HttpServletResponse response;

private File bookFile;
private String bookFileContentType;
private String bookFileFileName;

private String bookId;

@Resource(name="bookMgmtServiceImpl")
private IRBookMgmtService iBookService;

PhoenixProperties phoenixProp = PhoenixProperties.getInstance();

public void setiBookService(IRBookMgmtService iBookService) {
this.iBookService = iBookService;
}

public File getBookFile() {
return bookFile;
}

public void setBookFile(File bookFile) {
this.bookFile = bookFile;
}

public String getBookFileContentType() {
return bookFileContentType;
}

public void setBookFileContentType(String bookFileContentType) {
this.bookFileContentType = bookFileContentType;
}

public String getBookFileFileName() {
return bookFileFileName;
}

public void setBookFileFileName(String bookFileFileName) {
this.bookFileFileName = bookFileFileName;
}

public String getBookId() {
return bookId;
}

public void setBookId(String bookId) {
this.bookId = bookId;
}

public String uploadBook() throws Exception {

ActionContext.getContext().getParameters();

if (bookFile == null) {
throw new Exception("上传文件出错!");
}

FileInputStream fis = null;
FileOutputStream os = null;

StringBuffer outPath = new StringBuffer();
outPath.append(phoenixProp.getProperty("book_file_folder"));
outPath.append(File.separator);

RBook book = iBookService.findBook(bookId);
if (book == null) {
throw new Exception("数据库中无法找到目标书籍!");
}

outPath.append(book.getBookId());
outPath.append(File.separator);
outPath.append(bookFileFileName);


File file = new File(outPath.toString());
try {
if (file.exists()) {
file.delete();
}
fis = new FileInputStream(bookFile);
os = new FileOutputStream(file);
byte[] buffer = new byte[1024 * 16];
try {
while ((fis.read(buffer)) != -1) {
try {
os.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}

} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return "success";
}

@Override
public void setServletResponse(HttpServletResponse response) {
// TODO Auto-generated method stub
this.response = response;
}

@Override
public void setRequest(Map<String, Object> request) {
// TODO Auto-generated method stub
this.request = (RequestMap) request;
}
}


表单代码如下:

<form id="uploadBookFrm" enctype="multipart/form-data" method="POST" action="/phoenixCloud/book/uploadBook.do">
<span class="btn btn-default btn-file">
<span class="fileinput-new">选择书籍文件</span>
<span class="fileinput-exists">重新选择书籍文件</span>
<input id="bookFile" type="file" name="bookFile">
</span>
<span class="fileinput-filename">snap.sql</span>
<a class="close fileinput-exists" style="float: none" data-dismiss="fileinput" href="#">×</a>
<input type="hidden" value="1" name="bookId">
<input type="submit" value="submit" name="submit">
</form>


当Action的方法中被调用时,bookFile等属性为null,不知道什么原因?
struts.xml中配置如下:

<constant name="struts.multipart.saveDir" value="/tmp"/>
<constant name="struts.multipart.maxSize" value="104857600" />
...
<action name="uploadBook" class="bookUploadAction" method="uploadBook">
<interceptor-ref name="fileUpload">
<param name="maximumSize">104857600</param>
</interceptor-ref>
<result name="success" type="redirect">/book/bookDire_getAll.do?bookId=${bookId}</result>
</action>


bookUploadAction是通过Spring的@Component("
bookUploadAction")指定的Action的名称。换成完全限定名称,结果也是一样的。

不知道为什么 bookFile等属性为null????
...全文
173 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
jackey_option 2014-01-08
  • 打赏
  • 举报
回复
找到原因: 把struts.xml中拦截器去掉就可以了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧