4.struts2中的文件上传,下载
Struts2中文件的上传下载,是借用commons里面的包实现文件的上传,需要导入两个jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
实现的时候你可以使用s标签,也可以使用普通的表单标签步骤大概如下:
使用s标签的话,先导入标签
修改form 表单的enctype属性
编写action(利用IO流进行上传文件)
1.使用s标签的话,先导入标签
<%@ taglib prefix="s" uri="/struts-tags" %>
2.修改enctype属性
<body>
<s:form action="file2" method="post" enctype="multipart/form-data">
<s:file name="photo"></s:file></br>
<s:submit value="提交"></s:submit>
</s:form>
</body>
3.编写action(利用IO流进行上传文件)
属性:
private File photo;//文件位置
private String photoFileName;// 文件名
private String photoContentType;// 文件类型
上传方法:
public String execute() throws IOException {
// 传到哪里(文件长传后的路径)
ServletContext servletContext = ServletActionContext
.getServletContext();
String path = servletContext.getRealPath("/img/" + photoFileName);
System.out.println("文件路径:" + path);
// 去读取原文件的位置
FileInputStream in = new FileInputStream(photo);
// 写入你定义的文件上传的路径
FileOutputStream out = new FileOutputStream(path);
// 写的过程
byte[] buffer = new byte[1024];
int leng = 0;
// 就是已经读取文件完成
while ((leng = in.read(buffer)) != -1) {
out.write(buffer, 0, leng);
}
in.close();
out.close();
return "success";
}
文件下载直接贴代码:
在配置文件中的type属性使用stream作为type的结果类型
下载需要设置的参数:
contentType:
Contentlength:
inputName:指定getter定义的那个属性的名字,默认是inputStream
BufferSize:缓存大小
allowCache:是否允许缓存
contentCharSet:指定下载的字符集,在配置文件中配置一个type为stream的action
struts2配置文件
<!--下载的配置 -->
<action name="filedownload" class="com.etc.action.FileDownLoadAction" method="filedownload">
<result type="stream">
<param name="bufferSize">2048</param>
</result>
</action>
下载的代码(get,set方法记得生成)
private String contentType;
private String contentDisposition;
private InputStream inputStream;
private long contentLength;
public String filedownload() throws IOException{
contentType="text/html";//指定文件的类型
contentDisposition="attachment;filename=index.html";//下载的提示框
ServletContext servletContext=ServletActionContext.getServletContext();
//告诉文件在哪里
String filename=servletContext.getRealPath("/img/index.html");
//去读文件
inputStream=new FileInputStream(filename);
contentLength=inputStream.available();
return "success";
}
4.struts2中的文件上传,下载的更多相关文章
- java中的文件上传下载
java中文件上传下载原理 学习内容 文件上传下载原理 底层代码实现文件上传下载 SmartUpload组件 Struts2实现文件上传下载 富文本编辑器文件上传下载 扩展及延伸 学习本门课程需要掌握 ...
- struts2中的文件上传,文件下载
文件上传: Servlet中的文件上传回顾 前台页面 1.提交方式post 2.表单类型 multipart/form-data 3.input type=file 表单输入项 后台 apache提交 ...
- javaWeb中的文件上传下载
在Web应用系统开发中,文件上传和下载功能是非常常用的功能,今天来讲一下JavaWeb中的文件上传和下载功能的实现. 对于文件上传,浏览器在上传的过程中是将文件以流的形式提交到服务器端的,如果直接使用 ...
- struts2中的文件上传和下载
天下大事,必做于细.天下难事,必作于易. 以前见过某些人,基础的知识还不扎实就去学习更难的事,这样必定在学习新的知识会非常迷惑结果 再回来又一次学习一下没有搞懂的知识,这必定会导致学习效率的下降!我写 ...
- Java中实现文件上传下载的三种解决方案
第一点:Java代码实现文件上传 FormFile file=manform.getFile(); String newfileName = null; String newpathname=null ...
- struts2中的文件上传和文件下载
单文件文件上传 1.
- Struts2中实现文件上传的功能
1.首先得配置一下Struts得配置文件struts-xml: <?xml version="1.0" encoding="UTF-8" ?> &l ...
- javaweb项目中的文件上传下载功能的实现
框架是基于spring+myBatis的. 前台页面的部分代码: <form action="${ctx}/file/upLoadFile.do"method="p ...
- Linux中的文件上传下载
1.部署ftp服务器 2.安装bypy python 客户端(还没试过,先记录一下) https://www.v2ex.com/t/124886
随机推荐
- mybatis处理一对多的查询
//查询出某个班级对应的所有老师和学生 1.使用嵌套结果 <select id="findClasses3" parameterType="int" re ...
- .net 环境下get 获取页面出现乱码问题解决
不多说了,先上代码: /// <summary> /// 获取页面内容 /// </summary> /// <param name="Url"> ...
- Codeforces 28C [概率DP]
/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...
- oracle 创建用户
/*分为四步 *//*第1步:创建临时表空间 */create temporary tablespace ycjy tempfile 'D:\oracledata\ycjy.dbf' size 50 ...
- Grunt的使用
在Node环境下.需要预先安装好Node. 1.安装grunt-cli [root@Luxh-01 ~]# npm install -g grunt-cli 2.创建一个目录test [root@Lu ...
- 物料主数据MRP4中的独立/集中
转自悲守穷庐 http://blog.itpub.net/12287/viewspace-681569/ 从按订单还是按库存来考虑. (1)独立集中为空,即又上层决定独立集中情况 (2)独立集中为1: ...
- 获取Finacial dimension value的description 值
public static Description findDimensionValueDescription(DimensionValue _dimensionValue, Name _dimens ...
- unity自定义工具
using UnityEngine;using UnityEditor;using System.Collections;using System.IO;using System.Collection ...
- KindEditor 编辑器使用方法
http://kindeditor.net/docs/usage.html 编辑器使用方法 1. 下载编辑器 下载 KindEditor 最新版本,下载之后打开 examples/index.html ...
- C语言实现GPT头和分区表的读取(gcc)
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h&g ...