http://www.360doc.com/content/06/1106/18/12224_251687.shtml
http://www.360doc.com/content/06/0724/16/1916_163440.shtml
http://tmsoft.lsxy.com/index.php?load=read&id=514
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<html>
<head>
<title>Struts2 File Upload</title>
</head>
<body>
<form action="fileUpload.action" method="POST" enctype="multipart/form-data">
文件标题:<input type="text" name="title" size="50"/><br/>
选择文件:<input type="file" name="upload" size="50"/><br/>
<input type="submit" value=" 上传 "/>
</form>
</body>
</html>
|
package org.qiujy.web.struts2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
*处理文件上传的Action类
*
@authorqiujy
*
@version1.0
*/
publicclass FileUploadAction
extends ActionSupport {
privatestaticfinalint
BUFFER_SIZE = 16 * 1024;
// 文件标题
private String title;
// 上传文件域对象
private File upload;
// 上传文件名
private String uploadFileName;
// 上传文件类型
private String uploadContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
//以下省略getter和setter......
//自己封装的一个把源文件对象复制成目标文件对象
privatestaticvoid copy(File src, File dst) {
InputStream in =
null;
OutputStream out =
null;
try {
in =
new BufferedInputStream(
new FileInputStream(src),
BUFFER_SIZE);
out =
new BufferedOutputStream(
new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer =
newbyte[
BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (
null != in) {
try {
in.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (
null != out) {
try {
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public String execute()
throws Exception {
//根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = ServletActionContext.
getServletContext()
.getRealPath(
this.getSavePath())
+ "\\" +
this.getUploadFileName();
System.
out.println("上传的文件的类型:"+
this.getUploadContentType());
File dstFile =
new File(dstPath);
copy(
this.upload, dstFile);
return
SUCCESS;
}
}
|
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name ="fileUploadDemo" extends ="struts-default">
<action name ="fileUpload"
class ="org.qiujy.web.struts2.FileUploadAction">
<!-- 动态设置Action中的savePath属性的值 -->
<param name="savePath">/upload</param>
<result name ="success">/showupload.jsp</result>
</action >
</package >
</struts>
|
<!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.custom.i18n.resources" value="messages"/>
<package name="fileUploadDemo" extends="struts-default">
<action name="fileUpload"
class="org.qiujy.web.struts2.FileUploadAction">
<interceptor-ref name="fileUpload">
<!-- 配置允许上传的文件类型,多个用","分隔 -->
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpeg,image/jpg
,image/x-png, image/pjpeg
</param>
<!-- 配置允许上传的文件大小,单位字节 -->
<param name="maximumSize">102400</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
<!-- 动态设置Action中的savePath属性的值 -->
<param name="savePath">/upload</param>
<result name="input">/index.jsp</result>
<result name="success">/showupload.jsp</result>
</action>
</package>
</struts>
|
#更改上传文件类型不允许的提示信息
struts.messages.error.content.type.not.allowed=文件上传失败:你要上传的文件类型不允许
#更改上传文件太大的提示信息
struts.messages.error.file.too.large=文件上传失败:你要上传的文件太大
#文件上传其它错误信息
struts.messages.error.uploading=文件上传失败:发生内部错误
|
Unable to find 'struts.multipart.saveDir' property setting. Defaulting to javax.servlet.context.tempdir
Removing file upload D:\tomcat6.0.13\work\Catalina\localhost\fileload_struts2\upload__4b616fd1_115a3d5d9dc__7fff_00000005.tmp
|
<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<html>
<head>
<title>多文件上传</title>
</head>
<body>
<font color="red"><s:fielderror/></font>
<form action="multiFileUpload.action" method="POST" enctype="multipart/form-data">
文件标题:<input type="text" name="title" size="50" value="${param.title }"/><br/>
<!-- 设置二个文件域,名字相同 -->
选择第一个文件:<input type="file" name="upload" size="50"/><br/>
选择第二个文件:<input type="file" name="upload" size="50"/><br/>
<input type="submit" value=" 上传 "/>
</form>
</body>
</html>
|
package org.qiujy.web.struts2;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
*处理多文件上传的Action类
*
*
@authorqiujy
*
@version1.0
*/
publicclass MultiFileUploadAction
extends ActionSupport {
privatestaticfinalint
BUFFER_SIZE = 16 * 1024;
// 文件标题
private String title;
// 用File数组来封装多个上传文件域对象
private File[] upload;
// 用String数组来封装多个上传文件名
private String[] uploadFileName;
// 用String数组来封装多个上传文件类型
private String[] uploadContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
//以下为所有属性的getter和setter。省略。。。
// 自己封装的一个把源文件对象复制成目标文件对象
privatestaticvoid copy(File src, File dst) {
InputStream in =
null;
OutputStream out =
null;
try {
in =
new BufferedInputStream(
new FileInputStream(src),
BUFFER_SIZE);
out =
new BufferedOutputStream(
new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer =
newbyte[
BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
if (
null != in) {
try {
in.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
if (
null != out) {
try {
out.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Override
public String execute()
throws Exception {
File[] srcFiles =
this.getUpload();
// 处理每个要上传的文件
for (
int i = 0; i < srcFiles.length; i++) {
// 根据服务器的文件保存地址和原文件名创建目录文件全路径
String dstPath = ServletActionContext.
getServletContext()
.getRealPath(
this.getSavePath())
+ "\\" +
this.getUploadFileName()[i];
File dstFile =
new File(dstPath);
this.
copy(srcFiles[i], dstFile);
}
return
SUCCESS;
}
}
|

http://gdf84.iteye.com/blog/215885
http://hi.baidu.com/edmenzhang/blog/item/27887489ff91b1b00e24446d.html
struts上传文件
Struts的文件上传
本文用的是Struts1.1的org.apache.struts.upload.FormFile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。
选择上传文件页面:selfile.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/uploadsAction.do" enctype="multipart/form-data">
<html:file property="theFile"/>
<html:submit/>
</html:form>
</html:html>
最近刚做完一个项目,用Struts1.1做的。从不懂,到熟练使用,都靠参考优快云的一些文档。但是文章上讲的并不一定适合自己,所以我把我自己做的一些东西拿上来给大家看看,互相交流一下。如果您有跟好的方法,可以和我联系。
MSN:whw_dream (AT) hotmail.com
Struts的文件上传
本文用的是Struts1.1的org.apache.struts.upload.FormFile类。很方便,不用自己写。也不用写一个jsp调用jspsmartupload就可以搞定。
选择上传文件页面:selfile.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<html:html>
<html:form action="/uploadsAction.do" enctype="multipart/form-data">
<html:file property="theFile"/>
<html:submit/>
</html:form>
</html:html>
UpLoadAction.java
import java.io.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.FormFile;
/**
* <p>Title:UpLoadAction</p>
* <p>Description: QRRSMMS </p>
* <p>Copyright: Copyright (c) 2004 jiahansoft</p>
* <p>Company: jiahansoft</p>
* @author wanghw
* @version 1.0
*/
public class UpLoadAction extends Action {
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (form instanceof uploadsForm) {//如果form是uploadsForm
String encoding = request.getCharacterEncoding();
if ((encoding != null) && (encoding.equalsIgnoreCase("utf-8")))
{
response.setContentType("text/html; charset=gb2312");//如果没有指定编码,编码格式为gb2312
}
UpLoadForm theForm = (UpLoadForm ) form;
FormFile file = theForm.getTheFile();//取得上传的文件
try {
InputStream stream = file.getInputStream();//把文件读入
String filePath = request.getRealPath("/");//取当前系统路径
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream bos = new FileOutputStream(filePath + "/" +
file.getFileName());//建立一个上传文件的输出流
//System.out.println(filePath+"/"+file.getFileName());
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);//将文件写入服务器
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
//request.setAttribute("dat",file.getFileName());
return mapping.findForward("display");
}
return null;
}
}
UpLoadForm.java
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
/**
* <p>Title:UpLoadForm</p>
* <p>Description: QRRSMMS </p>
* <p>Copyright: Copyright (c) 2004 jiahansoft</p>
* <p>Company: jiahansoft</p>
* @author wanghw
* @version 1.0
*/
public class UpLoadForm extends ActionForm {
public static final String ERROR_PROPERTY_MAX_LENGTH_EXCEEDED = "org.apache.struts.webapp.upload.MaxLengthExceeded";
protected FormFile theFile;
public FormFile getTheFile() {
return theFile;
}
public void setTheFile(FormFile theFile) {
this.theFile = theFile;
}
public ActionErrors validate(ActionMapping mapping, HttpServletRequest request)
{
ActionErrors errors = null;
//has the maximum length been exceeded?
Boolean maxLengthExceeded = (Boolean)
request.getAttribute(MultipartRequestHandler.ATTRIBUTE_MAX_LENGTH_EXCEEDED);
if ((maxLengthExceeded != null) && (maxLengthExceeded.booleanValue()))
{
errors = new ActionErrors();
errors.add(ERROR_PROPERTY_MAX_LENGTH_EXCEEDED, new ActionError("maxLengthExceeded"));
}
return errors;
}
}
//这是相对应的form,还有其他属性可以设置,具体可以参考struts的上传例子。
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" " http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="uploadsForm" type="UpLoadForm" />
</form-beans>
<action-mappings>
<action name="uploadsForm" type="UpLoadAction" path="/uploadsAction">
<forward name="display" path="/display.jsp" />
</action>
</action-mappings>
</struts-config>
<!--display.jsp就是随便写一个成功页-->