ssh框架实现文件上传下载实例代码
最近在学习ssh框架,我用ssh框架写了一个文件上传下载的实例代码,有需要了解的朋友可参考。希望此文章对各位有所帮助。
<!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>
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; } }
<?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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)