zoukankan      html  css  js  c++  java
  • SPRING IN ACTION 第4版笔记-第七章Advanced Spring MVC-004- 处理上传文件

    一、用 MultipartFile

    1.在html中设置<form enctype="multipart/form-data">及<input type="file">

     1 <html xmlns:th="http://www.thymeleaf.org">
     2   <head>
     3     <title>Spitter</title>
     4     <link rel="stylesheet" type="text/css" 
     5           th:href="@{/resources/style.css}"></link>
     6   </head>
     7   <body>
     8     <div id="header" th:include="page :: header"></div>
     9 
    10     <div id="content">
    11       <h1>Register</h1>
    12   
    13       <form method="POST" th:object="${spitter}" enctype="multipart/form-data">
    14         <div class="errors" th:if="${#fields.hasErrors('*')}">
    15           <ul>
    16             <li th:each="err : ${#fields.errors('*')}" 
    17                 th:text="${err}">Input is incorrect</li>
    18           </ul>
    19         </div>
    20         <label th:class="${#fields.hasErrors('firstName')}? 'error'">First Name</label>: 
    21           <input type="text" th:field="*{firstName}"  
    22                  th:class="${#fields.hasErrors('firstName')}? 'error'" /><br/>
    23   
    24         <label th:class="${#fields.hasErrors('lastName')}? 'error'">Last Name</label>: 
    25           <input type="text" th:field="*{lastName}"
    26                  th:class="${#fields.hasErrors('lastName')}? 'error'" /><br/>
    27   
    28         <label th:class="${#fields.hasErrors('email')}? 'error'">Email</label>: 
    29           <input type="text" th:field="*{email}"
    30                  th:class="${#fields.hasErrors('email')}? 'error'" /><br/>
    31   
    32         <label th:class="${#fields.hasErrors('username')}? 'error'">Username</label>: 
    33           <input type="text" th:field="*{username}"
    34                  th:class="${#fields.hasErrors('username')}? 'error'" /><br/>
    35   
    36         <label th:class="${#fields.hasErrors('password')}? 'error'">Password</label>: 
    37           <input type="password" th:field="*{password}"  
    38                  th:class="${#fields.hasErrors('password')}? 'error'" /><br/>
    39 
    40         <label>Profile Picture</label>:
    41           <input type="file"
    42                  name="profilePicture"
    43                  accept="image/jpeg,image/png,image/gif" /><br/>
    44 
    45         <input type="submit" value="Register" />
    46       </form>
    47     </div>
    48     <div id="footer" th:include="page :: copy"></div>
    49   </body>
    50 </html>

    2.在实体bean中对应字段的类型设置为org.springframework.web.multipart.MultipartFile,以支持自动装配

     1 package spittr.web;
     2 
     3 import javax.validation.constraints.NotNull;
     4 import javax.validation.constraints.Size;
     5 
     6 import org.hibernate.validator.constraints.Email;
     7 import org.springframework.web.multipart.MultipartFile;
     8 
     9 import spittr.Spitter;
    10 
    11 public class SpitterForm {
    12 
    13   @NotNull
    14   @Size(min=5, max=16, message="{username.size}")
    15   private String username;
    16 
    17   @NotNull
    18   @Size(min=5, max=25, message="{password.size}")
    19   private String password;
    20   
    21   @NotNull
    22   @Size(min=2, max=30, message="{firstName.size}")
    23   private String firstName;
    24 
    25   @NotNull
    26   @Size(min=2, max=30, message="{lastName.size}")
    27   private String lastName;
    28   
    29   @NotNull
    30   @Email
    31   private String email;
    32   
    33   private MultipartFile profilePicture;

    3.在handler中保存

    (1)

     1 @RequestMapping(value="/register", method=POST)
     2   public String processRegistration(
     3       @Valid SpitterForm spitterForm,
     4       Errors errors) throws IllegalStateException, IOException {
     5     
     6     if (errors.hasErrors()) {
     7       return "registerForm";
     8     }
     9     Spitter spitter = spitterForm.toSpitter();
    10     spitterRepository.save(spitter);
    11     MultipartFile profilePicture = spitterForm.getProfilePicture();
    12     profilePicture.transferTo(new File(spitter.getUsername() + ".jpg"));
    13     return "redirect:/spitter/" + spitter.getUsername();
    14   }

    (2)也可以用RequestPart数组来接收图片数组

    1 @RequestMapping(value = "/register", method = POST)
    2 public String processRegistration(
    3     @RequestPart("profilePicture") byte[] profilePicture,
    4     @Valid Spitter spitter,
    5     Errors errors) {
    6     ...
    7 }

    If the user submits the form without selecting a file, then the array will be empty (but not  null ).

    (3)用MultipartFile类型的函数参数来接收图片

    1   @RequestMapping(method = RequestMethod.POST)
    2   public String processUpload(@RequestPart("file") MultipartFile profilePicture) {
    3       profilePicture.transferTo(new File("/data/spittr/" + profilePicture.getOriginalFilename()));
    4   }

    (4)把图片保存到Amazon S3

     1 private void saveImage(MultipartFile image)
     2 throws ImageUploadException {
     3     try {
     4         AWSCredentials awsCredentials = new AWSCredentials(s3AccessKey, s2SecretKey);
     5         S3Service s3 = new RestS3Service(awsCredentials);
     6         S3Bucket bucket = s3.getBucket("spittrImages");
     7         S3Object imageObject = new S3Object(image.getOriginalFilename());
     8         imageObject.setDataInputStream(image.getInputStream());
     9         imageObject.setContentLength(image.getSize());
    10         imageObject.setContentType(image.getContentType());
    11         AccessControlList acl = new AccessControlList();
    12         acl.setOwner(bucket.getOwner());
    13         acl.grantPermission(GroupGrantee.ALL_USERS, Permission.PERMISSION_READ);
    14         imageObject.setAcl(acl);
    15         s3.putObject(bucket, imageObject);
    16     } catch (Exception e) {
    17         throw new ImageUploadException("Unable to save image", e);
    18     }
    19 }

    The first thing that saveImage() does is set up Amazon Web Service ( AWS ) credentials. For this, you’ll need an S3 access key and an S3 secret access key. These will be given to you by Amazon when you sign up for S3 service. They’re provided to SpitterController via value injection.
    With the AWS credentials in hand, saveImage() creates an instance of JetS3t’s RestS3Service , through which it operates on the S3 filesystem. It gets a reference to the spitterImages bucket, creates an S3Object to contain the image, and then fills that S3Object with image data.
    Just before calling the putObject() method to write the image data to S3, saveImage() sets the permissions on the S3Object to allow all users to view it. This is important—without it, the images wouldn’t be visible to your application’s users.Finally, if anything goes wrong, an ImageUploadException will be thrown.

    4.在配置文件中设置好处理上传的bean和约束

      @Override
      protected void customizeRegistration(Dynamic registration) {
        registration.setMultipartConfig(
            new MultipartConfigElement("/tmp", 2097152, 4194304, 0));
      }

    二、用 javax.servlet.http.Part

    1.

    1 @RequestMapping(value = "/register", method = POST)
    2 public String processRegistration(
    3     @RequestPart("profilePicture") Part profilePicture,
    4     @Valid Spitter spitter,
    5     Errors errors) {
    6     ...
    7 }

    In many cases, the Part methods are named exactly the same as the MultipartFile methods. A few have similar but different names; getSubmittedFileName() , for example, corresponds to getOriginalFilename() . Likewise, write() corresponds to transferTo() , making it possible to write the uploaded file like this:

    profilePicture.write("/data/spittr/" + profilePicture.getOriginalFilename());

    It’s worth noting that if you write your controller handler methods to accept file uploads via a Part parameter, then you don’t need to configure the StandardServlet-MultipartResolver bean. StandardServletMultipartResolver is required only

    when you’re working with MultipartFile

  • 相关阅读:
    Python写一个简单的爬虫
    Fully Convolutional Networks for Semantic Segmentation 译文
    Numpy中矩阵和数组的区别
    Linux(ubuntu)软件的安装
    Linux 打包和压缩
    Linux的软连接、硬链接和find命令
    Linux组管理、用户管理、查看用户信息、usermod、which、切换用户、修改文件具体权限
    Linux用户权限
    Linux远程管理命令
    Linux文件和目录相关的命令
  • 原文地址:https://www.cnblogs.com/shamgod/p/5245458.html
Copyright © 2011-2022 走看看