多文件上传

在stucts中多文件上传的实现:

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。

第二步:编写一个提交的表单mulpfileupload.jsp

注意:其中多文件上传时name必须相同应为在接收的时候时以数组的形式接收滴

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/test/mulpupload.action" method="post">
<input type="file" name="uploadImages"><br/>
<input type="file" name="uploadImages"><br/>
<input type="file" name="uploadImages"><br/>
<input type="submit" value="提交"><br/>
</form>
<body>

</body>
</html>

第二步:编写Action

public class MulpUploadeFile {
//uploadImages这个和提交上name必须相同、多个文件上传

private File[] uploadImages;
//-ContentType文件类型
private String uploadImagesContentType;
///-FileName文件名、这里是数组因为上传了多个文件
private String[] uploadImagesFileName;
private String path;

public String getPath() {
return path;
}
public File[] getUploadImages() {
return uploadImages;
}
public void setUploadImages(File[] uploadImages) {
this.uploadImages = uploadImages;
}
public String getUploadImagesContentType() {
return uploadImagesContentType;
}
public void setUploadImagesContentType(String uploadImagesContentType) {
this.uploadImagesContentType = uploadImagesContentType;
}

public String[] getUploadImagesFileName() {
return uploadImagesFileName;
}
public void setUploadImagesFileName(String[] uploadImagesFileName) {
this.uploadImagesFileName = uploadImagesFileName;
}
public String upload()throws Exception
{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(realpath);
if(!file.exists()) file.mkdirs();
path=file.getAbsolutePath();
for(int i=0 ;i<uploadImages.length; i++){
File uploadImage = uploadImages[i];
FileUtils.copyFile(uploadImage,new File(file,uploadImagesFileName[i]));
}
return "success";
}
//文件所在路径F:\myeclipse\.metadata\.plugins\com.genuitec.eclipse.easie.tomcat.myeclipse\tomcat\webapps\Structs2\images
}


第三步:配置stucts.xml文件

<action name="mulpupload" class="com.liyong.MulpUploadeFile.MulpUploadeFile" method="upload" >
<result name="success">/WEB-INF/page/success.jsp</result>
<!-- 访问路径 http://localhost:8080/Structs2/test/sevletScope -->
</action>

第四步:部署

第五步:访问//http://localhost:8080/Structs2/mulpfileupload.jsp

.....
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值