• struts2上传文件


    上传文件在一个系统当中是一个很常用的功能,也是一个比较重要的功能。今天我们就一起来学习一下Struts2如何上传文件。
    今天讲的上传文件的方式有三种:
    1,以字节为单位传输文件;
    2,Struts2封装的一种方式;
    3,以字符的方式传输文件。

    其实这三种方式都差不多,都是将文件先从客户端一临时文件的形式,传输到服务器的临时文件夹下,然后在将该临时文件复制到我们要上传的目录。另外,有一个需要注意,就是上传过程中产生的这些临时文件,Struts2不会自动清理,所以我们需要手动清理临时文件,这一个下面的代码中有提到。

    用Action来完成我们上传的核心功能:
    [java] view plaincopy在CODE上查看代码片派生到我的代码片
    package com.action;

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.util.Map;

    import org.apache.struts2.ServletActionContext;

    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;


    public class UploadAction extends ActionSupport {

    private File upload;
    private String uploadFileName;
    private String uploadContentType;

    private long maximumSize;
    private String allowedTypes;


    public File getUpload() {
    return upload;
    }
    public void setUpload(File upload) {
    this.upload = upload;
    }
    public String getUploadFileName() {
    return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = uploadFileName;
    }

    public String getUploadContentType() {
    return uploadContentType;
    }
    public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
    }
    public long getMaximumSize() {
    return maximumSize;
    }
    public void setMaximumSize(long maximumSize) {
    this.maximumSize = maximumSize;
    }
    public String getAllowedTypes() {
    return allowedTypes;
    }
    public void setAllowedTypes(String allowedTypes) {
    this.allowedTypes = allowedTypes;
    }
    @Override
    public String execute() throws Exception {

    File uploadFile = new File(ServletActionContext.getServletContext().getRealPath("upload"));
    if(!uploadFile.exists()) {
    uploadFile.mkdir();
    }

    //验证文件大小及格式
    if (maximumSize < upload.length()) {
    return "error";
    }

    boolean flag =false;
    String[] allowedTypesStr = allowedTypes.split(",");
    for (int i = 0; i < allowedTypesStr.length; i++) {
    if (uploadContentType.equals(allowedTypesStr[i])) {
    flag = true;
    }
    }
    if (flag == false) {
    Map request = (Map) ActionContext.getContext().get("request");
    request.put("errorMassage", "文件类型不合法!");

    System.out.println(request.toString());
    return "error";
    }

    //第一种上传方式
    // FileInputStream input = new FileInputStream(upload);
    // FileOutputStream out = new FileOutputStream(uploadFile +"//"+ uploadFileName);
    // try {
    // byte[] b =new byte[1024];
    // int i = 0;
    // while ((i=input.read(b))>0) {
    // out.write(b, 0, i);
    // }
    // } catch (Exception e) {
    // e.printStackTrace();
    // } finally {
    // //关闭输入、输出流
    // input.close();
    // out.close();
    // //删除临时文件
    // upload.delete();
    // }

    //第二种上传方式
    // FileUtils.copyFile(upload, new File(uploadFile+"\"+uploadFileName));
    // //删除临时文件
    // upload.delete();

    //第三种上传方式
    BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(upload)));
    BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadFile+"\"+ uploadFileName)));
    try {
    char [] c =new char[1024];
    int i = 0;
    while ((i = bReader.read(c)) > 0) {
    bWriter.write(c, 0, i);
    }
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    bReader.close();
    bWriter.close();
    //删除临时文件
    upload.delete();
    }


    return "success";
    }

    }

  • 相关阅读:
    解决.net core 3.1 json日期带T的问题
    C# 将指定得时间转换成秒
    WPF Button按钮的按下和抬起分别触发的事件
    WPF Image图片后台绑定图片,不对图片照成占用
    WPF Textbox漂亮的文本控件框封装
    WPF Slider实现可拖动的时间轴
    .net mvc 下显示PDF文件
    .net core 3.1使用命令自动生成MySQL表对应的实体类
    .net core A generic error occurred in GD 已解决
    WPF 模拟Button按钮事件触发
  • 原文地址:https://www.cnblogs.com/wangyonglong/p/5178443.html
Copyright © 2020-2023  润新知