文件上传下载struts2

上传方式1:

  

	// 保存上传的文件
	public boolean saveFile(File file, String fileName) throws IOException {
		File newFile = new File(this.UFILE_ROOT_PATH);
		if (!newFile.exists())
			newFile.mkdirs();
		newFile = new File(this.UFILE_ROOT_PATH, fileName);

		FileOutputStream os = null;
		FileInputStream is = null;
		try {
			os = new FileOutputStream(newFile);
			is = new FileInputStream(this.getFileUpload());
		} catch (FileNotFoundException e) {
			os.close();
			is.close();
		}

		byte[] b = new byte[1024];
		int length = 0;

		try {
			while (-1 != (length = is.read(b, 0, 1024))) {
				os.write(b, 0, 1024);
			}
		} catch (IOException e) {
			os.close();
			is.close();
		}

		os.close();
		is.close();
		return true;
	}

  上传方式2:

            String targetDirectory = ServletActionContext.getServletContext()
                    .getRealPath("/"+ UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim());// 获得路径
        
            for (int i = 0; i < upload.length; i++)
            {
                String fileName = uploadFileName[i];// 上传的文件名
                String type = uploadContentType[i];// 文件类型
                String realName = UUID.randomUUID().toString()+ getExt(fileName);// 保存的文件名称,使用UUID+后缀进行保存

                
                File target = new File(targetDirectory, realName);
                 FileUtils.copyFile(upload[i], target);// 上传至服务器的目录,一般都这样操作,
                                                        // 在把路径写入数据库即可

                UploadFiles uf = new UploadFiles();// 创建文件
                uf.setUploadContentType(type);
                uf.setUploadFileName(fileName);
                uf.setUploadRealName(realName);

                uploadFiles.add(uf);// 添加到需要下载文件的List集合中

            }
            ServletActionContext.getRequest().setAttribute("uploadFiles",
                    uploadFiles);

下载方式:

package org.usc.file;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.usc.utils.UploadConfigurationRead;

public class DownloadAction extends ActionSupport
{
    private static final long serialVersionUID = 6329383258366253255L;
    private String fileName;
    private String fileRealName;
    public void setFileName()
    {
        // 得到请求下载的文件名
        String fname = ServletActionContext.getRequest().getParameter("name");
        String frealname = ServletActionContext.getRequest().getParameter("realname");
        try
        {
            /*
             * 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。
             * 这里使用request.setCharacterEncoding解码无效.
             * 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件
             */
            fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
            frealname= new String(frealname.getBytes("ISO-8859-1"), "UTF-8");
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        this.fileName = fname;
        this.fileRealName = frealname;
//        System.out.println(fileName);
//        System.out.println(fileRealName);
    }

    /*
     * @getFileName 此方法对应的是struts.xml文件中的: <param
     * name="contentDisposition">attachment;filename="${fileName}"</param>
     * 这个属性设置的是下载工具下载文件时显示的文件名, 要想正确的显示中文文件名,我们需要对fileName再次编码
     * 否则中文名文件将出现乱码,或无法下载的情况
     */
    public String getFileName() throws UnsupportedEncodingException
    {

        fileRealName = new String(fileRealName.getBytes(), "ISO-8859-1");

        return fileRealName;
    }

    /*
     * @getDownloadFile 此方法对应的是struts.xml文件中的: <param
     * name="inputName">downloadFile</param> 返回下载文件的流,可以参看struts2的源码
     */
    public InputStream getDownloadFile()
    {

        this.setFileName();
        return ServletActionContext.getServletContext().getResourceAsStream("/"+UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim()+"/" + fileName);
    }

    @Override
    public String execute() throws Exception
    {
        return SUCCESS;
    }
}
原文地址:https://www.cnblogs.com/mynona/p/3257195.html