当前位置:首页 » 文件管理 » struts2上传下载

struts2上传下载

发布时间: 2023-01-14 10:49:50

‘壹’ 1、struts2 如何上传文件并显示在jsp页面上,提供下载连接 2、struts2 如何下载显示在jsp页面上的文件

http://hi..com/kawnj19890209/blog/item/8c64d78e139c88f4f11f36fa.html可以看看这儿

‘贰’ struts2中,文件上传和下载时候的缓冲区大小多少合适如题 谢谢了

文件通常默认是15MB的允许最大上传的文件,缓冲其实设为4096K就好了。和文件大小没关系。只跟网速度有关,设大了也没用。

‘叁’ struts2上传下载

07和03版的EXCEL用的是不同的扩展名,如果你用POI之类的jar包处理office文档的话,处理这两种格式文件的方法也是不一样的

‘肆’ struts2 文件的上传和下载,怎么控制上传的文件类型啊

<interceptor-ref name="fileUpload">
<param name="allowedTypes">application/octet-stream,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel,application/msexcel</param>
<param name="maximumSize">10048576</param>
</interceptor-ref>
具体内容去struts教程网看看吧。

‘伍’ struts2实现图片的上传和下载

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.*;
import com.zdvictory.taurus.common.util.*;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;

/** *
*/
public class UploadFileHandler {

private static int BUFFER_SIZE = 8192;

/**
* 上传附件操作 传递参数:系统参数配置设置的参数名称
*/
@SuppressWarnings("unchecked")
public static List<Attachment> upload(String sysParaName) throws Exception {
// 文件保存路径
String path = SysParaFinder.getSysParaValue(sysParaName);
List<Attachment> list = new ArrayList<Attachment>();
MultiPartRequestWrapper request = (MultiPartRequestWrapper) ServletActionContext
.getRequest();
Enumeration enu = request.getFileParameterNames();
while (enu.hasMoreElements()) { // 对每一个文件域进行遍历
String controlName = (String) enu.nextElement();
String[] fileNames = request.getFileNames(controlName);
File[] uploadFiles = request.getFiles(controlName);
for (int i = 0; i < uploadFiles.length; i++) {
File uploadFile = uploadFiles[i];
if (!uploadFile.exists())
continue;
// 如果文件夹不存在,创建文件夹,将文件保存到目录
File dir = new File(request.getRealPath("/") + path);
if (!dir.exists())
dir.mkdirs();
String ext = fileNames[i].substring(fileNames[i].indexOf("."),
fileNames[i].length());// 获取文件扩展名
String filename = UUID.randomUUID().toString() + ext;
File file = new File(request.getRealPath("/") + path + filename);
byte[] data = new byte[BUFFER_SIZE];
int byteRead = -1;
FileInputStream in = new FileInputStream(uploadFile);
FileOutputStream out = new FileOutputStream(file);
while ((byteRead = in.read(data)) != -1) {
out.write(data, 0, byteRead);
out.flush();
}
out.close();
in.close();
// 设置附件对象属性
Attachment attach = new Attachment();
attach.setFilename(fileNames[i]);
attach.setContentType(ext);
attach.setFilepathname(path + filename);
attach.setFilesize(uploadFile.length());
list.add(attach);
}
}
return list;
}
}
文件下载

public String download() throws Exception {
redheadTemplate = redheadTemplateManager.findById(Long
.valueOf(getId()[0]));
String name = redheadTemplate.getName()
+ redheadTemplate.getFilepathname().substring(
redheadTemplate.getFilepathname().lastIndexOf("."),
redheadTemplate.getFilepathname().length());
this.setFilename(new String(name.getBytes(), "ISO8859-1"));
this.setFilepathname(redheadTemplate.getFilepathname());
return "download";
}
文件下载配置文件

<result name="download" type="stream">
<!-- 下载文件类型 -->
<param name="contentType">
application/octet-stream
</param>
<!-- 默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文
件保存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名,
这里使用的是动态文件名,${filename}, 它将通过 Action 的 getFilename() 获得文件名 -->
<param name="contentDisposition">
attachment;filename="${filename}"
</param>
<!-- 下载的InputStream流,Struts2自动对应Action中的getDownloadFile方法,该方法必须返回InputStream类型 -->
<param name="inputName">downloadFile</param>
<!-- 输出时缓冲区的大小 -->
<param name="bufferSize">8192</param>
</result>

‘陆’ struts2文件上传和下载

上传:

package com.ljq.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
publicclass UploadAction2 extends ActionSupport {

// 封装上传文件域的属性
private File image;
// 封装上传文件类型的属性
private String imageContentType;
// 封装上传文件名的属性
private String imageFileName;
// 接受依赖注入的属性
private String savePath;

@Override
public String execute() {
FileOutputStream fos =null;
FileInputStream fis =null;
try {
// 建立文件输出流
System.out.println(getSavePath());
fos =new FileOutputStream(getSavePath() +"\\"+ getImageFileName());
// 建立文件上传流
fis =new FileInputStream(getImage());
byte[] buffer =newbyte[1024];
int len =0;
while ((len = fis.read(buffer)) >0) {
fos.write(buffer, 0, len);
}
} catch (Exception e) {
System.out.println("文件上传失败");
e.printStackTrace();
} finally {
close(fos, fis);
}
return SUCCESS;
}

/**
* 返回上传文件的保存位置
*
* @return
*/
public String getSavePath() throws Exception{
return ServletActionContext.getServletContext().getRealPath(savePath);
}

publicvoid setSavePath(String savePath) {
this.savePath = savePath;
}

public File getImage() {
return image;
}

publicvoid setImage(File image) {
this.image = image;
}

public String getImageContentType() {
return imageContentType;
}

publicvoid setImageContentType(String imageContentType) {
this.imageContentType = imageContentType;
}

public String getImageFileName() {
return imageFileName;
}

publicvoid setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}

privatevoid close(FileOutputStream fos, FileInputStream fis) {
if (fis !=null) {
try {
fis.close();
} catch (IOException e) {
System.out.println("FileInputStream关闭失败");
e.printStackTrace();
}
}
if (fos !=null) {
try {
fos.close();
} catch (IOException e) {
System.out.println("FileOutputStream关闭失败");
e.printStackTrace();
}
}
}
}

‘柒’ Struts.2的文件上传下载为什么要继承ActionSupport类

不继承这个类也同样可以实现的。但是习惯上继承这个类。

热点内容
恩布拉科压缩机冰箱 发布:2025-09-05 11:04:55 浏览:897
如何把安卓手机号码移到苹果上 发布:2025-09-05 11:04:08 浏览:235
我的世界服务器怎么开外挂中国版 发布:2025-09-05 10:53:26 浏览:608
php数据导入excel 发布:2025-09-05 10:52:47 浏览:618
如何配置系统主机名 发布:2025-09-05 10:42:52 浏览:281
androidlistview头部 发布:2025-09-05 10:42:44 浏览:773
c语言字符串数组的 发布:2025-09-05 10:35:51 浏览:168
畅玩安卓模拟器怎么用 发布:2025-09-05 10:30:13 浏览:643
猫吃饭解压 发布:2025-09-05 10:25:09 浏览:467
dbca创建数据库 发布:2025-09-05 10:24:12 浏览:18