Struts2.0上传初步学习

这两天看了一下struts2.0的上传,实现一个简单的例子很简单,我是从strtus2.0自带的一个例子看的,也没有经过修改就可以实现上传了,但下载还是出现了一些问题。最开始接触的时候出现了一个问题,也就是包的问题或者说是配置的问题,在web.xml里面仅仅是配置了struts的映射,导致每次启动的时候说找不着一个东西,然后又发现包存在问题,因为使用的是myEclipse,在里面选的加入自定义的一个userLibery库,貌似这样是不行,以前也碰到这种情况,那个时候还不清楚,像这一次,我把那几个需要的包直接添加进去之后就可以运行了。其实也有可能触发此问题的是和其它的一些包起冲突,没有去证明这个推断的正确性。
下面就说一下怎么实现这上传的功能吧。
首先是把包给加入进去,参考了一下网上的例子,因此就只加入了一下几个包
[code]
xwork-2.04.jar
commons-loggin-1.0.4.jar
ognl-2.6.11.jar
struts2-core-1.011.jar
commons-fileupload-1.1.1.jar
commons-io-1.1.jar [/code]
web.xml的配置如下:
[code]<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter>
<filter-name>struts-cleanup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts-cleanup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>

</web-app>[/code]
struts.xml的配置如下:
[code]<?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">

<!-- START SNIPPET: xworkSample -->
<struts>
<include file="struts-fileupload.xml" />
</struts>[/code]
struts-fileupload.xml配置如下:
[code]<?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>
<package name="fileupload" extends="struts-default" namespace="/fileupload">

<action name="upload" class="com.struts.fileupload.FileUploadAction" method="input">
<result>upload.jsp</result>
</action>

<action name="doUpload" class="com.struts.fileupload.FileUploadAction" method="upload">
<result name="input">upload.jsp</result>
<result>upload-success.jsp</result>
</action>

<action name="multipleUploadUsingList">
<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">1024000000</param>
</interceptor-ref>
<param name="savePath">/temp</param>
<result>multipleUploadUsingList.jsp</result>
</action>

<action name="doMultipleUploadUsingList" class="com.struts.fileupload.MultipleFileUploadUsingListAction" method="upload">
<result>multipleUploadUsingList-success.jsp</result>
</action>


<action name="multipleUploadUsingArray">
<result>multipleUploadUsingArray.jsp</result>
</action>

<action name="doMultipleUploadUsingArray" class="com.struts.fileupload.MultipleFileUploadUsingArrayAction" method="upload">
<result>multipleUploadUsingArray-success.jsp</result>
</action>


</package>
</struts>[/code]
fileUploadAction.java如下:
[code]
package com.struts.fileupload;

import java.io.File;

import com.opensymphony.xwork2.ActionSupport;

/**
* Show case File Upload example's action. <code>FileUploadAction</code>
*
*/
public class FileUploadAction extends ActionSupport {

private static final long serialVersionUID = 5156288255337069381L;

private String contentType;
private File upload;
private String fileName;
private String caption;

// since we are using <s:file name="upload" .../> the file name will be
// obtained through getter/setter of <file-tag-name>FileName
public String getUploadFileName() {
return fileName;
}
public void setUploadFileName(String fileName) {
this.fileName = fileName;
}


// since we are using <s:file name="upload" ... /> the content type will be
// obtained through getter/setter of <file-tag-name>ContentType
public String getUploadContentType() {
return contentType;
}
public void setUploadContentType(String contentType) {
this.contentType = contentType;
}


// since we are using <s:file name="upload" ... /> the File itself will be
// obtained through getter/setter of <file-tag-name>
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}


public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}

public String input() throws Exception {
return SUCCESS;
}

public String upload() throws Exception {
return SUCCESS;
}

}
[/code]
upload.jsp如下:
[code]<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase</title>
</head>

<body>
<h1>Fileupload sample</h1>

<s:actionerror />
<s:fielderror />
<s:form action="doUpload" method="POST" enctype="multipart/form-data">
<s:fielderror></s:fielderror>
<s:file name="upload" label="File"/>
<s:textfield name="caption" label="Caption"/>
<s:submit />
</s:form>
</body>
</html>

[/code]
upload-success.jsp如下:

[code]<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase</title>
</head>

<body>
<h1>Fileupload sample</h1>


<ul>
<li>ContentType: <s:property value="uploadContentType" /></li>
<li>FileName: <s:property value="uploadFileName" /></li>
<li>File: <s:property value="upload" /></li>
<li>Caption:<s:property value="caption" /></li>
</ul>


</body>
</html>

[/code]
这样单一的上传文件就好了。
下面是多文件上传的代码,MultipleFileUploadUsingListAction.java代码如下:
[code]package com.struts.fileupload;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

/**
* Showcase action - multiple file upload using List
* @version $Date: 2006-11-23 12:31:52 -0500 (Thu, 23 Nov 2006) $ $Id: MultipleFileUploadUsingListAction.java 478625 2006-11-23 17:31:52Z wsmoak $
*/
public class MultipleFileUploadUsingListAction extends ActionSupport {

private List<File> uploads = new ArrayList<File>();
private List<String> uploadFileNames = new ArrayList<String>();
private List<String> uploadContentTypes = new ArrayList<String>();


public List<File> getUpload() {
return this.uploads;
}
public void setUpload(List<File> uploads) {
this.uploads = uploads;
}

public List<String> getUploadFileName() {
return this.uploadFileNames;
}
public void setUploadFileName(List<String> uploadFileNames) {
this.uploadFileNames = uploadFileNames;
}

public List<String> getUploadContentType() {
return this.uploadContentTypes;
}
public void setUploadContentType(List<String> contentTypes) {
this.uploadContentTypes = contentTypes;
}


public String upload() throws Exception {

System.out.println("\n\n upload1");
System.out.println("files:");
for (File u: uploads) {
System.out.println("*** "+u+"\t"+u.length());
}
System.out.println("filenames:");
for (String n: uploadFileNames) {
System.out.println("*** "+n);
}
System.out.println("content types:");
for (String c: uploadContentTypes) {
System.out.println("*** "+c);
}
System.out.println("\n\n");
return SUCCESS;
}


}
[/code]
multipleFileUploadUsingList.jsp如下:
[code]<%@ page
language="java"
contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Showcase - fileupload - multiple fileupload using List</title>
</head>
<body>

<s:form action="doMultipleUploadUsingList" method="POST" enctype="multipart/form-data">
<s:file label="File (1)" name="upload" />
<s:file label="File (2)" name="upload" />
<s:file label="FIle (3)" name="upload" />
<s:submit />
</s:form>

</body>
</html>[/code]
multipleFileUploadUsingList-success.jsp如下:

[code]<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>

<table border="1">
<s:iterator value="upload" status="stat">
<tr>
<td>File <s:property value="%{#stat.index}" /></td>
<td><s:property value="%{upload[#stat.index]}" /></td>
</tr>
</s:iterator>
</table>


<table border="1">
<s:iterator value="uploadFileName" status="stat">
<tr>
<td>File Name <s:property value="%{#stat.index}" /></td>
<td><s:property value="%{uploadFileName[#stat.index]}" /></td>
</tr>
</s:iterator>
</table>

<table border="1">
<s:iterator value="uploadContentType" status="stat">
<tr>
<td>Content Type <s:property value="%{#stat.index}" /></td>
<td><s:property value="%{uploadContentType[#stat.index]}" /></td>
</tr>
</s:iterator>
</table>

</body>
</html>[/code]
这样就可以了。
现在还存在着一些问题,不知道怎么去限制文件上传类型,确定上传目录,限制上传的大小这个还不知道怎么设置,在相应的action里面做如下配置就是不行,也不知道为啥。
[code]<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">1024000000</param>
</interceptor-ref>[/code]
不知道是不是和tomcat6有关。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值