Struts2文件上传

public class UploadAction extends BaseAction{

    private static final long serialVersionUID = 1L;

    private File file;
    private String fileName;
    private String fileType;

    @Action(value = "/backend/upload/uploadvideo")
    public void uploadvideo() {
        JSONObject json = new JSONObject();//

        try {
            String filePath = CommonUpload.upLoad(fileName,fileType,file);
            json.accumulate("success", true);
            json.accumulate("filePath", filePath);
            json.accumulate("fileName", videoFileFileName);
        } catch (IOException e) {
            json.accumulate("success", false);
            json.accumulate("errorMsg", "上传失败! 原因为"+e.getMessage());
        }
        AjaxResponse.responseOut(json);
    }
}
public class CommonUpload {

    //单独的文件服务器路径
//  public static final String UPLOAD_ROOT = "upload/backend/info/upload";
    //与项目在同一路径下
    public static final String UPLOAD_ROOT = "backend/info/upload";
    public static final int BUFFER_SIZE = 1024;
    public static String upLoad(String fileName, String pictureType, File file) throws IOException {
            SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMdd/HH");
            String ext = fileName.substring(fileName.lastIndexOf('.') + 1).toLowerCase();
            String type;
            if(ext.equals("jpg")||ext.equals("jpeg")||ext.equals("png")||ext.equals("gif")||ext.equals("bmp")){
                type="image";//图片
            }else if(ext.equals("mp4")||ext.equals("avi")){
                type="video";//视频
            }else{
                type="brochure";//文档
            }


            String pathdir = "/" + UPLOAD_ROOT + "/" + pictureType + "/" +"/" + type + "/" + dateformat.format(new Date()); //构建文件保存的目录

            //与项目在同一路径下
//          String pathwebapp = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("");
//          pathwebapp = pathwebapp.substring(0,pathwebapp.lastIndexOf("\\"));
//          System.out.println(path);
//          String realpathdir = pathwebapp+pathdir;


            //与项目在同一路径下
            String realpathdir = ServletActionContext.getRequest().getSession().getServletContext().getRealPath(pathdir);


            File savedir = new File(realpathdir);
            if (!savedir.exists()) {
                savedir.mkdirs(); //如果目录不存在就创建
            }
            String filename = UUID.randomUUID().toString() + "." + ext; //构建文件名称
            String path = pathdir + "/" + filename;
            FileOutputStream fileoutstream = new FileOutputStream(new File(realpathdir, filename));
            FileInputStream fis = new FileInputStream(file);
            byte[] buffer = new byte[BUFFER_SIZE];
            int len = 0;
            while ((len = fis.read(buffer)) > 0) {
                fileoutstream.write(buffer, 0, len);
            }
            fileoutstream.close();
            fis.close();
            return path;
        }
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值