zoukankan      html  css  js  c++  java
  • struts上传与下载

    1.上传

    (1).Action

     1 public class Register extends ActionSupport{
    2 private FileuploadTool tool=new FileuploadTool();
    3 public FileuploadTool getTool() {
    4 return tool;
    5 }
    6 public void setTool(FileuploadTool tool) {
    7 this.tool = tool;
    8 }
    9 @Override
    10 public String execute() throws Exception {
    11 tool.beginUpload();
    12 return Action.INPUT;
    13 }
    14 }

    (2)FileuploadTool定义如下:

    public class FileuploadTool {
    private File[] uploadFile;
    private String[] uploadFileFileName;
    private String[] uploadFileContentType;
    //...省略getter setter

    public void beginUpload() throws Exception{
    String dir=ServletActionContext.getServletContext()
    .getRealPath("/upload");//上传文件的保存路径
    for(int i=0;i<uploadFile.length;i++){
    String srcName=uploadFileFileName[i];
    int p=srcName.lastIndexOf(".");
    String suffix=srcName.substring(p+1, srcName.length());
    String pureName=srcName.substring(0, p);
    String fileName=srcName;
    File target;
    int count=1;
    while(true){
    target=new File(dir,fileName);
    if(target.exists())
    fileName=pureName+"("+(count++)+")."+suffix;
    else break;
    }
    FileUtils.copyFile(uploadFile[i], target);
    }

    }
    }

    (3).jsp页面表单设置

    1 <s:form action="register" method="post" enctype="multipart/form-data" >
    2 <!--name属性值必须与Action一致-->
    3 <s:file name="tool.uploadFile"></s:file>
    4 <s:submit value="submit"/>
    5 </s:form>

    2.下载

    (1).action

     1 public class Download extends ActionSupport{
    2 private String filename="";
    3
    4 public String getFilename() {
    5 return filename;
    6 }
    7
    8 public void setFilename(String filename) {
    9 this.filename = filename;
    10 }
    11 public InputStream getDownloadFile() throws Exception{
    12 //转码使得filename中的中文可以正确接收
    13 filename=new String(filename.getBytes("ISO8859-1"));
    14 FileInputStream fis=new FileInputStream(
    15 ServletActionContext.getServletContext()
    16 .getRealPath("/"+filename)
    17 );
    18 return fis;
    19 }
    20 //用于在浏览器下载显示的文件名 转换为iso8859-1编码
    21 public String getDownloadFileName() throws Exception{
    22 return new String(filename.getBytes(),"ISO8859-1");
    23 }
    24 @Override
    25 public String execute() throws Exception {
    26 // TODO Auto-generated method stub
    27 return "success";
    28 }
    29 }

    (2).action配置

    1 <action name="dd" class="com.control.Download" >
    2 <result name="success" type="stream">
    3 <param name="inputName">downloadFile</param>
    4 <param name="bufferSize">1024</param>
    5 <param name="contentDisposition">attachment;filename="${downloadFileName}"</param>
    6 <param name="contentType">application/x-msdownload</param>
    7 </result>
    8 </action>

    (3).jsp页面

    <a href="dd.action?filename=<%= URLEncoder.encode("中文.doc") %>">点此下载</a>





     


     




  • 相关阅读:
    [转帖]VI使用手册
    hadoop安装配置
    永远的beyond!(4 days left to get back touch)
    求比较+围观(3 days left to get back touch)
    Windows及其他软件开发过程中一般都有哪些版本?
    程序员的7个坏习惯
    回来真好,,,
    Windows8 consumer preview的第一次
    那些年,备胎一起追的女神
    准备开始CP之旅。。。。(DP is Over!)
  • 原文地址:https://www.cnblogs.com/tazi/p/2294987.html
Copyright © 2011-2022 走看看