zoukankan      html  css  js  c++  java
  • 【SSH网上商城项目实战13】Struts2实现文件上传功能

    转自:https://blog.csdn.net/eson_15/article/details/51366384

    上一节我们做完了添加和更新商品的功能,这两个部分里有涉及到商品图片的上传,并没有详细解说。为此,这篇文章详细介绍一下Struts2实现文件上传的功能。

    1. 封装文件信息
            我们首先得有一个Model来封装文件的信息,这个Model里需要有三个属性:文件、文件类型和文件名。针对我们要传的图片,我们新建一个Model如下:

     1 public class FileImage {
     2     private File file;
     3     private String contentType;
     4     private String filename;
     5     
     6     public File getFile() {
     7         return file;
     8     }
     9  
    10     public String getContentType() {
    11         return contentType;
    12     }
    13  
    14     public String getFilename() {
    15         return filename;
    16     }
    17  
    18     public void setUpload(File file) { //set方法可以不用和属性名一样,但是前台传进来时的参数得和set方法名相同。即前台传的参数为fileImage.upload
    19         this.file = file;
    20     }
    21     
    22     public void setUploadContentType(String contentType) {
    23         this.contentType = contentType;
    24     }
    25     
    26     public void setUploadFileName(String filename) {
    27         this.filename = filename;
    28     }
    29 }

    这样Model就写好了,考虑到文件上传的逻辑不是单个Action所特有的,所以我们将文件上传的逻辑写到工具类中,这样可供所有的Action调用。所以我们新建一个文件上传工具类(为了面向接口编程,我们也将工具类抽出个接口):

    2. 完成文件上传工具类

     1 //文件上传工具类接口
     2 public interface FileUpload {
     3  
     4     //实现文件上传的功能,返回上传后新的文件名称
     5     public abstract String uploadFile(FileImage fileImage);
     6  
     7 }
     8  
     9 //文件上传工具类具体实现
    10 @Component("fileUpload")
    11 public class FileUploadUtil implements FileUpload {
    12     
    13     private String filePath;
    14     @Value("#{prop.filePath}") 
    15     //@Value表示去beans.xml文件中找id="prop"的bean,它是通过注解的方式读取properties配置文件的,然后去相应的配置文件中读取key=filePath的值
    16     public void setFilePath(String filePath) {
    17         System.out.println(filePath);
    18         this.filePath = filePath;
    19     }
    20  
    21     //1. 通过文件名获取扩展名
    22     private String getFileExt(String fileName) {
    23         return FilenameUtils.getExtension(fileName);
    24     }
    25     
    26     //2. 生成UUID随机数,作为新的文件名
    27     private String newFileName(String fileName) {
    28         String ext = getFileExt(fileName);
    29         return UUID.randomUUID().toString() + "." + ext;
    30     }
    31     
    32     //实现文件上传的功能,返回上传后新的文件名称
    33     @Override
    34     public String uploadFile(FileImage fileImage) {
    35         //获取新唯一文件名
    36         String pic = newFileName(fileImage.getFilename());
    37         try {
    38             FileUtil.copyFile(fileImage.getFile(), new File(filePath, pic));//第一个参数是上传的文件,第二个参数是将文件拷贝到新路径下
    39             return pic;
    40         } catch (Exception e) {
    41             throw new RuntimeException(e);
    42         } finally {
    43             fileImage.getFile().delete();
    44         }
    45     }
    46 }

    上面有个@Value注解,是从properties文件中获取文件要存入的路径的,具体可参见:Spring获取配置文件信息 。

    3. 在Action中注入封装文件类和工具类
            写好了文件封装类和上传文件工具类后,我们需要将这两个对象注入到我们的Action中,这样就可以在Action中实现文件上传的功能了:

     1 @Controller("baseAction")
     2 @Scope("prototype")
     3 public class BaseAction<T> extends ActionSupport implements RequestAware,SessionAware,ApplicationAware,ModelDriven<T> {
     4  
     5     //封装了图片信息的类
     6     protected FileImage fileImage;
     7     
     8     //上传文件工具类
     9     @Resource
    10     protected FileUpload fileUpload;
    11  
    12     public FileImage getFileImage() {
    13         return fileImage;
    14     }
    15     public void setFileImage(FileImage fileImage) {
    16         this.fileImage = fileImage;
    17     }
    18     //省略其他无关代码……
    19 }

    4. 实现文件的上传

                 好了,现在我们可以在ProductAction中去实现文件上传了,工具类写好的话,在Action中的代码量就很少了,这也是封装带来的优点。

     1 @Controller("productAction")
     2 @Scope("prototype")
     3 public class ProductAction extends BaseAction<Product> {
     4  
     5         //省略其他无关代码……
     6     
     7     public void save() throws Exception {
     8         //fileUpload工具类被抽取了,uploadFile方法直接接受一个fileImage对象,返回新的图片名
     9         String pic = fileUpload.uploadFile(fileImage);
    10         
    11         model.setPic(pic);
    12         model.setDate(new Date());
    13         System.out.println(model);
    14         //商品信息入库
    15         productService.save(model);
    16     }
    17     
    18     public void update() {
    19         String pic = fileUpload.uploadFile(fileImage);
    20         model.setPic(pic);
    21         model.setDate(new Date());
    22         System.out.println(model);
    23         //更新商品
    24         productService.update(model);
    25     }
    26 }

           这样我们就完成了从前台上传文件的功能。

  • 相关阅读:
    oracle中Blob和Clob类型的区别
    为什么要分库分表
    Enable file editing in Visual Studio's debug mode
    SQL Server Dead Lock Log
    Debug .NET Framework Source
    SQL Server text field里面有换行符的时候copy到excel数据会散乱
    诊断和修复Web测试记录器(Web Test Recorder)问题
    Can't load Microsoft.ReportViewer.ProcessingObjectModel.dll
    'telnet' is not recognized as an internal or external command
    Linq to XML
  • 原文地址:https://www.cnblogs.com/sharpest/p/9850157.html
Copyright © 2011-2022 走看看