最近在学习ssh框架,我用ssh框架写了一个文件上传下载的实例代码,有需要了解的朋友可参考。希望此文章对各位有所帮助。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!doctype html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta name = "Author" content = "BuNuo" >
< meta name = "Keywords" content = "文件上传下载" >
< meta name = "Description" content = "ssh框架文件上传下载" >
< title >Document</ title >
</ head >
< body >
< form action = "newFile" method = "post" enctype = "multipart/form-data" >
< input type = "file" name = "file" >
< input type = "submit" value = "上传" >
</ form >
< a href = "downFile?filename=${filename}" rel = "external nofollow" >下载</ a >
</ body >
</ html >
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
package com.cpsec.tang.chemical.action;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller ( "newFileAction" )
public class NewFileAction extends ActionSupport {
private File file; //文件
private String fileFileName; //文件名称
private String fileContentType; //文件类型
private String filename;
private InputStream inputStream;
public String newFile(){
File dir= new File(ServletActionContext.getServletContext().getRealPath( "files" ));
//判断文件是否上传,如果上传的话将会创建该目录
if (!dir.exists()){
dir.mkdir(); //创建该目录
}
System.out.println(file);
System.out.println(fileFileName);
//第一种文件上传的方法
//声明文件输入流,为输入流指定文件路径
//获取输出流,获取文件的文件地址及名称
FileInputStream in= null ;
FileOutputStream out= null ;
try {
in= new FileInputStream(file);
out= new FileOutputStream(dir + "\\" +fileFileName);
byte [] b= new byte [ 1024 * 1024 ]; //每次写入的大小
int i= 0 ;
while ((i=in.read(b))> 0 ){
out.write(b, 0 ,i);
}
in.close();
out.close();
} catch (Exception e){
e.printStackTrace();
} finally {
}
/*//第二种文件上传的方法
try {
FileUtils.copyFile(file,new File(dir,fileFileName));
} catch (IOException e) {
e.printStackTrace();
}
//第三种方法
BufferedReader bReader=null;
BufferedWriter bWriter=null;
try{
bReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
bWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dir+"\\"+fileFileName)));
char[] str=new char[1024*1024];
int i=0;
while((i=bReader.read(str))>0){
bWriter.write(str,0,i);
}
bReader.close();
bWriter.close();
dir.delete();
}catch(Exception e){
e.printStackTrace();
}finally{
} */
return SUCCESS;
}
public String downFile(){
System.out.println(filename);
inputStream=ServletActionContext.getServletContext().getResourceAsStream( "/files/" +filename);
System.out.println(inputStream);
return SUCCESS;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this .file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this .fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this .fileContentType = fileContentType;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this .filename = filename;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this .inputStream = inputStream;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<? xml version = "1.0" encoding = "UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
< struts >
< package name = "newFileAction" namespace = "/" extends = "struts-default" >
< action name = "downFile" class = "newFileAction" method = "downFile" >
< result name = "success" type = "stream" >
< param name = "contentType" >application/octet-stream;charset=ISO8859-1</ param >
<!-- inputName 流对象名 —— 比如这里写inputStream,它就会自动去找Action中的getInputStream方法。 -->
< param name = "inputName" >inputStream</ param >
<!-- contentDisposition 使用经过转码的文件名作为下载文件名 —— 默认格式是attachment;filename="${fileName}",将调用该Action中的getFileName方法。 -->
< param name = "contentDisposition" >attachment;filename=${filename}</ param >
<!-- bufferSize 下载文件的缓冲大小 -->
< param name = "bufferSize" >4096</ param >
</ result >
</ action >
</ package >
</ struts >
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/bunuo/p/6185312.html