单文件上传

上传页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="upload.action" method="post" enctype="multipart/form-data">
  11. 文件:<input type="file" name="upload"><br><br>
  12. 上传者:<input type="text" name="author">
  13. <input type="submit" value="上传">
  14. </form>
  15. </body>
  16. </html>

struts.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  4. "http://struts.apache.org/dtds/struts-2.5.dtd">
  5. <struts>
  6. <constant name="struts.devMode" value="true"></constant>
  7. <package name="jiangwenwen" namespace="/" extends="struts-default">
  8. <action name="upload" class="cn.jiangwenwen.action.UploadAction" method="fileUpload">
  9. <result name="success">/success.jsp</result>
  10. </action>
  11. </package>
  12. </struts>

Action类

  1. public class UploadAction extends ActionSupport{
  2. //存放上传的文件对象
  3. private File upload;
  4. //上传的文件名称
  5. private String uploadFileName;
  6. //上传的上传者
  7. private String author;
  8. public String fileUpload() throws IOException {
  9. FileInputStream fis = new FileInputStream(upload);
  10. String path = "D://pic/"+uploadFileName;
  11. FileOutputStream fos = new FileOutputStream(path);
  12. int flag = 0;
  13. while((flag=fis.read())!=-1) {
  14. fos.write(flag);
  15. }
  16. fis.close();
  17. fos.close();
  18. return SUCCESS;
  19. }
  20. public File getUpload() {
  21. return upload;
  22. }
  23. public void setUpload(File upload) {
  24. this.upload = upload;
  25. }
  26. public String getUploadFileName() {
  27. return uploadFileName;
  28. }
  29. public void setUploadFileName(String uploadFileName) {
  30. this.uploadFileName = uploadFileName;
  31. }
  32. public String getAuthor() {
  33. return author;
  34. }
  35. public void setAuthor(String author) {
  36. this.author = author;
  37. }
  38. }

成功接收页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8" isELIgnored="false"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <!-- 此 /pic为服务器配置的路径-->
  11. <img src="/pic/${uploadFileName }">
  12. </body>
  13. </html>

批量上传

上传页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <form action="upload.action" method="post" enctype="multipart/form-data">
  11. 文件:<input type="file" name="upload"><br><br>
  12. <input type="file" name="upload"><br><br>
  13. 上传者:<input type="text" name="author">
  14. <input type="submit" value="上传">
  15. </form>
  16. </body>
  17. </html>

struts.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE struts PUBLIC
  3. "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
  4. "http://struts.apache.org/dtds/struts-2.5.dtd">
  5. <struts>
  6. <!-- 修改文件上传大小限制 -->
  7. <constant name="struts.multipart.maxSize" value="11111111111111111"></constant>
  8. <!-- 开启国际化,value的值是配置文件的名称(在src目录下)-->
  9. <constant name="struts.custom.i18n.resources" value="message" />
  10. <!-- 开启开发者模式 -->
  11. <constant name="struts.devMode" value="true"></constant>
  12. <package name="jiangwenwen" namespace="/" extends="struts-default">
  13. <action name="upload" class="cn.jiangwenwen.action.UploadAction" method="upload">
  14. <result name="success">/success.jsp</result>
  15. <result name="input">/error.jsp</result>
  16. <!-- 控制单个文件上传大小 -->
  17. <interceptor-ref name="fileUpload">
  18. <param name="maximumSize">
  19. 1500
  20. </param>
  21. </interceptor-ref>
  22. <interceptor-ref name="defaultStack"></interceptor-ref>
  23. </action>
  24. </package>
  25. </struts>

Action类

  1. public class UploadAction extends ActionSupport{
  2. //上传的文件对象
  3. private File[] upload;
  4. //上传的文件名称
  5. private String[] uploadFileName;
  6. //上传的文件类型
  7. private String[] uploadContentType;
  8. public String upload() {
  9. String path = "D://pic/";
  10. for(int i=0;i<upload.length;i++) {
  11. upload[i].renameTo(new File(path,uploadFileName[i]));
  12. System.out.println("上传成功");
  13. }
  14. return SUCCESS;
  15. }
  16. public File[] getUpload() {
  17. return upload;
  18. }
  19. public void setUpload(File[] upload) {
  20. this.upload = upload;
  21. }
  22. public String[] getUploadFileName() {
  23. return uploadFileName;
  24. }
  25. public void setUploadFileName(String[] uploadFileName) {
  26. this.uploadFileName = uploadFileName;
  27. }
  28. public String[] getUploadContentType() {
  29. return uploadContentType;
  30. }
  31. public void setUploadContentType(String[] uploadContentType) {
  32. this.uploadContentType = uploadContentType;
  33. }
  34. }

成功接收页面

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <%@ taglib prefix="s" uri="/struts-tags"%>
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <s:debug></s:debug>
  12. </body>
  13. </html>

Struts2之上传的更多相关文章

  1. FTP文件操作之上传文件

    上传文件是一个比较常用的功能,前段时间就做了一个上传图片的模块.开始采用的是共享文件夹的方式,后来发现这种方法不太好.于是果断将其毙掉,后来选择采用FTP的方式进行上传.个人感觉FTP的方式还是比较好 ...

  2. Unable to find ‘struts.multipart.saveDir’ Struts2上传文件错误的解决方法

    Unable to find ‘struts.multipart.saveDir’ Struts2上传文件错误的解决方法 在使用struts2的项目中上传文件的时候出现了一个这样的错误: 2011-7 ...

  3. [JavaWeb基础] 009.Struts2 上传文件

    在web开发中,我们经常遇到要把文件上传下载的功能,这篇文章旨在指导大家完成文件上传功能 1.首先我们需要一个上传文件的页面. <!--在进行文件上传时,表单提交方式一定要是post的方式, 因 ...

  4. 利用struts2上传文件时,如果文件名中含有-符号,那么会出错

    利用struts2上传文件时,如果文件名中含有-符号,那么会出错 报错如下: HTTP Status 500 - C:\Program Files\Apache Software Foundation ...

  5. struts2上传的问题

    5. 在这里我加一个struts2的一个上传验证的问题 上传时我们可以这样来验证 //判断上传的文件是否合要求 public boolean filterType(String []types){ / ...

  6. struts2上传文件添加进度条

    给文件上传添加进度条,整了两天终于成功了. 想要添加一个上传的进度条,通过分析,应该是需要不断的去访问服务器,询问上传文件的大小.通过已上传文件的大小, 和上传文件的总长度来评估上传的进度. 实现监听 ...

  7. 关于Struts2上传文件的最大Size的设置

    今天使用Struts2的文件上传控件时,在struts.xml中,将处理上传的action中的fileUpload拦截器的maximumSize参数设置为5000000,上传了一个3M的文件后发现控制 ...

  8. struts2上传下载

    struts上传下载必须引入两个jar文件: commons-fileupload-x.x.x.jar和comons-io-x.x.x.jar上传文件 import java.io.BufferedI ...

  9. 菜鸟学SSH(五)——Struts2上传文件

    上传文件在一个系统当中是一个很常用的功能,也是一个比较重要的功能.今天我们就一起来学习一下Struts2如何上传文件. 今天讲的上传文件的方式有三种: 1,以字节为单位传输文件: 2,Struts2封 ...

随机推荐

  1. mongoDB学习笔记(2)

    一.删数据库 1.语法 MongoDB 删除数据库的语法格式如下: db.dropDatabase() 删除当前数据库,默认为 test,你可以使用 db 命令查看当前数据库名. 2.实例 以下实例我 ...

  2. Vue 实现文件的下载

    上次说了,实现文件的上传需要三步,那么实现文件的下载呢? 答:也是三步 第一步:获取文件的 fileId (或者别的什么的,总之应该是代表这个文件的东西),各家后台需要的都不一样 第二步:调用接口 t ...

  3. php WebService应用

    <?php header ( "Content-Type: text/html; charset=gb2312" ); /* * 指定WebService路径并初始化一个We ...

  4. .NET Reactor使用教程(加密源代码示例)

    更多:https://www.cnblogs.com/PiaoMiaoGongZi/category/1120300.html 1.打开 Eziriz .NET Reactor,主界面如图1所示: 图 ...

  5. 求助:关于shell数值比较的错误提示

    今天写了个脚本,过不了错误这一关,求大神路过瞟一眼. 1 #!/bin/bash 2 #disk use 3 disk_use() { 4 DISK_LOG=/tmp/disk_use.tmp 5 D ...

  6. openprocess打不开 如何读取exe路径描述

    openprocess打不开 如何读取exe路径描述 openprocess打不开 如何读取exe路径描述 https://bbs.pediy.com/thread-210652.htm https: ...

  7. oralce 汇编02

    Assembler Directives .align integer, padThe .align directive causes the next data generated to be al ...

  8. AOP切面详解

    一.spring-aop.xml文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns= ...

  9. idea hibernate console 执行hql报错

    报错信息 hql> select a from GDXMZD a[2019-08-29 13:45:01] org.hibernate.service.spi.ServiceException: ...

  10. 编译原理--NFA/DFA

    现成的, 讲义: https://www.cnblogs.com/AndyEvans/p/10240790.html https://www.cnblogs.com/AndyEvans/p/10241 ...