首页 > 代码库 > 文件上传下载删除

文件上传下载删除

<form action="newFile" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
<a href="downFile?filename=${filename}">下载</a><br/><br/><br/>
<a href="deleteFile?filename=${filename}">删除</a><br/><br/><br/>
<?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="newFilesAction" namespace="/" extends="struts-default">
    
        <action name="newFile" class="newFilesAction" method="newFile">
            <result name="success">NewFile.jsp</result>
        </action>
        
        <action name="downFile" class="newFilesAction" 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>
            <result name="error">NewFile.jsp</result>
         </action>
         
         <action name="deleteFile" class="newFilesAction" method="deleteFile">
            <result name="success">NewFile.jsp</result>
        </action>
    </package>
</struts>
package com.cpsec.tang.chemical.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.UUID;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;

@Controller("newFilesAction")
public class NewFileAction extends ActionSupport {
    private static final long serialVersionUID = 4765816351070218273L;
    private File file;// 文件
    private String fileFileName;// 文件名称
    private String fileContentType; // 文件类型
    private String filename;
    private InputStream inputStream;
    private String webpath;

    public String newFile() {
        System.out.println(file);
        System.out.println(fileFileName);
        String[] Name = this.getFileFileName().split("\\.");
        String fileExt = Name[Name.length - 1];// 后缀名
        String UUIDValue = http://www.mamicode.com/UUID.randomUUID().toString();// 随机字符串(文件名)
        StringBuffer path = new StringBuffer("/");// 随机路径
        for (int n = 0; n < UUIDValue.length(); n++) {
            char c = UUIDValue.charAt(n);
            path.append(c + "/");
        }
        System.out.println(path);
        webpath="files" + path;//相对路径
        System.out.println("webpath="+webpath);
        
        //存放文件的绝对路径(不包括文件)
        File dir = new File(ServletActionContext.getServletContext().getRealPath(webpath));
        // 判断文件是否上传,如果上传的话将会创建该目录
        if (!dir.exists()) {
            dir.mkdirs(); // 创建该目录
        }

        fileFileName = UUIDValue + "." + fileExt;
        filename = fileFileName;
        System.out.println("fileName" + filename);
        //存放文件的相对路径(包括文件),此路径存储在数据库中
        String filepath=webpath+"\\"+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 {
        }

        return SUCCESS;
    }

    //下载文件
    public String downFile() {
        File f=new File(ServletActionContext.getServletContext().getRealPath(webpath)+"\\"+filename);
        if(f.exists()){
            System.out.println(f.toString());
            inputStream = ServletActionContext.getServletContext().getResourceAsStream(webpath + "\\" + filename);
        System.out.println(inputStream);
            return SUCCESS;
        }else{
            return ERROR;
        }
    }
    
    
    //删除文件
    public String deleteFile() {
        String lujing=ServletActionContext.getServletContext().getRealPath(webpath)+"\\"+filename;
        System.out.println(lujing);
        File f=new File(lujing);
        if (f.exists()){
            System.out.println(f);
            f.delete();
        }
        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;
    }

    public String getWebpath() {
        return webpath;
    }

    public void setWebpath(String webpath) {
        this.webpath = webpath;
    }

}

 

文件上传下载删除