1.单文件上传
Action类:
public class UploadAction {
private File file;//用于接收表单上传文件
//xxxFileName为文件名,xxxContentType为文件类型,xxx为File变量名
private String fileFileName;
private String fileContentType;
public String execute() throws IOException{
//特别注意这里的getRealPath获取的路径是WebRoot下的文件夹路径
File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),fileFileName);
InputStream in = null;
OutputStream out = null;
try{
in = new FileInputStream(file);
out = new FileOutputStream(saved);
byte[] b = new byte[1024];
while((in.read(b)) != -1){
out.write(b);
}
}catch(Exception e){
}finally{
in.close();
out.close();
}
return "success";
}
public String getFile() {
return file;
}
public void setFile(String 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;
}
sturts.xml:
<struts>
<package name="main" extends="struts-default" namespace="/">
<action name="upload" class="com.sbw.action.UploadAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
login.jsp:
//注意上传文件需设置enctype和method为post
<s:form action="upload" enctype="multipart/form-data" method="post">
<s:file name="file" label="文件"></s:file>
<s:submit value="开始上传"/>
</s:form>
success.jsp:
文件已保存到:<a href="upload/<s:property value="fileFileName"/>" target="_blank"><s:property value="fileFileName"/></a>
运行结果:
2.多文件上传
Action类:
public class UploadAction {
private File[] file;//用于接收表单上传文件
//xxxFileName为文件名,xxxContentType为文件类型,xxx为File变量名
private String[] fileFileName;
private String[] fileContentType;
public String execute() throws IOException{
//特别注意这里的getRealPath获取的路径是WebRoot下的文件夹路径
InputStream in = null;
OutputStream out = null;
try{
for(int i = 0; i < file.length; i++){
File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),fileFileName[i]);
in = new FileInputStream(file[i]);
out = new FileOutputStream(saved);
byte[] b = new byte[1024];
while((in.read(b)) != -1){
out.write(b);
}
}
}catch(Exception e){
}finally{
in.close();
out.close();
}
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;
}
login.jsp:
<s:form action="upload" enctype="multipart/form-data" method="post">
<s:file name="file" label="文件1"></s:file>
<s:file name="file" label="文件2"></s:file>
<s:submit value="开始上传"/>
</s:form>
success.jsp:
文件已保存到:<a href="upload/<s:property value="fileFileName[0]"/>" target="_blank"><s:property value="fileFileName[0]"/></a><br/>
文件已保存到:<a href="upload/<s:property value="fileFileName[1]"/>" target="_blank"><s:property value="fileFileName[1]"/></a>
运行结果:
3.文件下载
Action类:
public class DownloadAction {
private String path;
public String execute(){
return "success";
}
//方法名去掉get后的downloadFile要与struts.xml中配置的inputName相同
public InputStream getDownloadFile(){
//此处获得的路径是在Webroot根目录下的
InputStream in = ServletActionContext.getServletContext().getResourceAsStream(getPath());
return in;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
sturts.xml:
<struts>
<package name="main" extends="struts-default" namespace="/">
<action name="download" class="com.sbw.action.DownloadAction">
<param name="path">download/123.txt</param>
<result name="success" type="stream">
<!-- 指定下载文件的类型 -->
<param name="contentType">image/gif</param>
<!-- 与action中getDownloadFile()去去掉get相同 -->
<param name="inputName">downloadFile</param>
<!-- 指定下载文件的位置 -->
<param name="contentDisposition">attachement;filename="123.txt"</param>
<!-- 指定下载文件的缓冲大小 -->
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>
login.jsp
<a href="download.action">文件下载</a>
运行结果: