package file;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class FileUpLoadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
// 定义缓冲的大小
private static final int BUFFER_SIZE = 1024 * 8;
// 文件标题
private String title;
// 用File数组来封装多个上传文件域对象
private File[] upload;
// 用String数组来封装多个上传文件名
private String[] uploadFileName;
// 用String数组来封装多个上传文件类型
private String[] uploadContentType;
// 保存文件的目录路径(通过依赖注入)
private String savePath;
// 自己封装的一个把源文件对象复制成目标文件对象
public static boolean copy(File src, File dst) {
boolean result = false;
InputStream in = null;
OutputStream out = null;
try {
in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream(new FileOutputStream(dst),
BUFFER_SIZE);
byte[] buffer = new byte[BUFFER_SIZE];
int len = 0;
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len); // buffer缓冲区 0 : 从第一个字开始,len 是每次输出多少
}
result = true;
} catch (Exception e) {
e.printStackTrace();
result = false;
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
// 文件为什么只能在盘符的根目录下才能传 否则乱码
public String fileLoad() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
request.setCharacterEncoding("utf-8");
// 从页面上的得到值
File[] srcFiles = this.getUpload();
// 保存文件名
List<String> successFileList = new ArrayList<String>();
// 读取文件
if (srcFiles != null) {
for (int i = 0; i < srcFiles.length; i++) {
// 根据服务器的文件保存地址和原文件名创建目录文件全路径 dstPath 输出的路径
String dstPath = ServletActionContext.getServletContext()
.getRealPath("/upload")
+ "\\"
+ new String(
this.getUploadFileName()[i].getBytes("utf-8"),
"GBK");
System.out.println("----------------------");
System.out.println("文件:" + srcFiles[i]);
System.out.println("路径:" + dstPath);
File dstFile = new File(dstPath);
if (copy(srcFiles[i], dstFile)) {
System.out.println("文件名:"
+ new String(this.getUploadFileName()[i]
.getBytes("utf-8"), "GBK"));
successFileList.add(new String(this.getUploadFileName()[i]
.getBytes("utf-8"), "GBK"));
}
}
request.setAttribute("successFileList", successFileList);
return SUCCESS;
}
return INPUT;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public File[] getUpload() {
return upload;
}
public void setUpload(File[] upload) {
this.upload = upload;
}
public String[] getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String[] uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String[] getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String[] uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getSavePath() {
return savePath;
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
UploadAndDown.zip (33个子文件)
UploadAndDown
.project 389B
.settings
org.eclipse.jdt.core.prefs 629B
src
struts.dtd 3KB
struts.xml 1KB
file
FileUpLoadAction.java 4KB
FileDownloadAction.java 469B
webapp
WEB-INF
filedown.jsp 910B
web.xml 573B
lib
classes
struts.dtd 3KB
struts.xml 1KB
file
FileDownloadAction.class 849B
FileUpLoadAction.class 5KB
ok.jsp 583B
upload
006.jpg 8KB
noodle4.jpg 4KB
defalut.jpg 3KB
nophoto.jpg 3KB
noodle1.jpg 4KB
noodle2.jpg 6KB
002.jpg 9KB
noodle3.jpg 5KB
oli3.jpg 3KB
001.jpg 6KB
Struts2 应用开发详解(第三讲).ppt 13KB
oli2.jpg 4KB
sis.jpg 18KB
sis1.jpg 41KB
oli1.jpg 4KB
sis2.jpg 18KB
fileup.jsp 667B
no.jsp 229B
filedownload.jsp 294B
.classpath 879B
共 33 条
- 1
资源评论
- xyg8882012-12-11一般般,没啥效果
- hehhs02012-11-21哥们, 真的很难猜到怎么去用,怎么不在主页弄点儿超级链接呢
- a6447424892015-12-04一个简单到例子
窗外雨寒
- 粉丝: 20
- 资源: 4
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功