您的位置:首页 > 编程语言 > Java开发

struts2实现文件上传(验证过!)

2011-03-18 09:40 501 查看
jsp(因为我是要做一个wap项目,所以dtd头跟有些不一样):

<?xml version="1.0" encoding="UTF-8"?>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="application/vnd.wap.xhtml+xml; charset=utf-8" />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<title>The FileUpload Demo</title>
</head>

<body>
<%out.print(basePath); %>
<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>
</body>
</html>

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;
}
}

struts.xml:

<?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>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- 加载spring配置文件(as:applicationContext.xml) -->
<listener>
<listener-class>com.start.util.SpringHelper</listener-class>
</listener>

<!-- 指明spring配置文件在何处 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/spring.xml</param-value>
</context-param>

<!-- 加载struts2核心 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/m/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/l/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/c/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/page/*</url-pattern>
</filter-mapping>
-->
<!-- JSTL -->
<jsp-config>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>http://java.sun.com/jstl/fmt</taglib-uri>
<taglib-location>/WEB-INF/fmt.tld</taglib-location>
</taglib>
</jsp-config>
<session-config>
<session-timeout>240</session-timeout>
</session-config>
</web-app>
web.xml有加载了我自己的东西,用的时候要把不需要的删掉。

测试时候当时有个问题:

java.io.FileNotFoundException: /tmp/upload__3e019d96_12ec6707d88__7fff_00000001.tmp (系统找不到指定的文件。)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(FileInputStream.java:106)
com.start.action.FileUpLoadAction.fileUpload(FileUpLoadAction.java:62)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)

我 的错误是这样的,我测试时因为规定了各式,就新建了个文本文件,改了下各式,但是文件大小是0字节,结果报错了,改个有大小的就ok了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: