2

我创建了一个基本的用户系统,人们可以在其中注册和登录。

它此时存储文本数据,但我想扩展代码以允许用户上传自己的图像。

我正在使用 mongodb,因此我可以在创建用户时将用户 ID 作为文件夹存储,因为他们添加自己的图像时它将进入该文件夹。

1.如何动态创建这些文件夹以及在哪里。我看过的文件夹通常会存储在WEB-INF中。

或者应该/可以将图像存储为 mongodb 中的 blob 文件?

2.如何在正确的位置正确地将图像复制/存储到这些文件夹中。

我已经尝试从这里调整代码http://www.roseindia.net/tutorial/spring/spring3/web/spring-3-mvc-fileupload-example.html

它目前与用户无关,也没有将图像组织到文件夹中。最终我想自动缩放/裁剪图像以产生各种缩略图大小。


文件上传.java

package net.fileupload;

import org.springframework.web.multipart.MultipartFile;
public class FileUpload {
 MultipartFile file;
 public void setFile(MultipartFile file){
  this.file=file;
  }
 public MultipartFile getFile(){
  return file;
  }
 }


文件上传控制器.java

package net.fileupload;

        import javax.servlet.http.HttpServletRequest;
        import javax.servlet.http.HttpServletResponse;
        import org.springframework.validation.BindException;
        import org.springframework.web.multipart.MultipartFile;
        import org.springframework.web.servlet.ModelAndView;
        import org.springframework.web.servlet.mvc.SimpleFormController;
        import java.io.InputStream;
        import java.io.OutputStream;
        import java.io.FileOutputStream;
        import java.util.*;

        import net.fileupload.FileUpload;;

        public class FileUploadController extends SimpleFormController{

          @Override
          protected ModelAndView onSubmit(HttpServletRequest request,
                   HttpServletResponse response, Object command, BindException errors)
                   throws Exception {
             FileUpload fileUpload = (FileUpload)command;
             MultipartFile multipartFile = fileUpload.getFile();
             String fileName="";

             // image type of file processing...
             System.err.println("-------------------------------------------");
             try {
               InputStream inputStream = null;
               OutputStream outputStream = null;
               if (multipartFile.getSize() > 0) {
                  inputStream = multipartFile.getInputStream();
                  fileName = request.getRealPath("") + "/images/"+ multipartFile.getOriginalFilename();
                  System.out.println(fileName);
                  outputStream = new FileOutputStream(fileName);
                  int readBytes = 0;
                  byte[] buffer = new byte[2000000];
                  while ((readBytes = inputStream.read(buffer, 0, 2000000)) != -1) {
                     outputStream.write(buffer, 0, readBytes);
                   }
                  outputStream.close();
                  inputStream.close();
                }       
              } catch (Exception e) {
                 e.printStackTrace();
              }
             // .......................................... 
             Map model = new HashMap();
             model.put("fileName", multipartFile.getOriginalFilename());
             model.put("filepath", "images/"+multipartFile.getOriginalFilename());

             return new ModelAndView(getSuccessView(), model);
           }
         }


FileUploadValidator.java

package net.fileupload;

    import org.springframework.validation.Errors;
    import org.springframework.validation.Validator;
    import net.fileupload.FileUpload;

    public class FileUploadValidator implements Validator{
        @Override
        public boolean supports(Class clazz){
            return FileUpload.class.isAssignableFrom(clazz);
         }  
        @Override
        public void validate(Object target, Errors errors){
          FileUpload fileUpload = (FileUpload)target;
          if(fileUpload.getFile().getSize()==0){
               errors.rejectValue("file", "error.empty.file", "Please Select File.");
            }
          System.out.println(fileUpload.getFile().getSize());

          if(fileUpload.getFile().getSize() > 2000000){
               errors.rejectValue("file", "error.empty.file", "File size more than 2000000 bytes ");
            }
        }
    }

检索图像。

理论上,以下内容应返回与用户相关的图像列表。不妨将这些图像作为 json 数组返回。


画廊控制器

import java.util.ArrayList;

        import java.util.List; 

        import com.mongodb.BasicDBObject;

        import com.mongodb.DBObject;


        public class GalleryController{


            public static List<DBObject> getGallery(){

                //__Prepare response
                List<DBObject> images = new ArrayList<DBObject>();
                BasicDBObject results = new BasicDBObject();

                BasicDBObject result = new BasicDBObject();

                    result.put("image1", "/images/Bannan.jpg");
                    result.put("image2", "/images/Koala.jpg");
                    result.put("image3", "/images/Monkey.jpg");

                    results.put("images", result);
                    images.add(results);

                return images;
            }

        }


侦听器控制器


/* * Gallery methods */

    @RequestMapping(method=RequestMethod.GET, value={"/gallery"})
    public ModelAndView galleryDisplay(
            HttpServletRequest request, 
            HttpServletResponse response,
            @RequestParam(value="mode", required=false) String mode
        ) {
            String message = "search view for "+mode;


            List<DBObject> images = GalleryController.getGallery();
            System.out.println(images);

            String viewPage = "gallery/galleryview";

            return new ModelAndView(viewPage, "images", images);
    }

4

1 回答 1

0

我当前的用户控制器看起来像这样。

如果我可以在这个级别为用户连接图像并将其提供给模板,那就太好了。

我当前的映射在以 id 作为参数的情况下效果很好。本地主机:8080/user?id=1312321321

/*
* User
*/
@RequestMapping(method=RequestMethod.GET, value={"/user","/user/{id}"})
public ModelAndView profileDisplay(
HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value="id", required=false) String id
) throws UnknownHostException, MongoException {
ServiceSerlvet.appendSesssion(request);
//get search ALL users
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("_id", new ObjectId(id));
List<DBObject> searchResponse = 
PersonController.searchUsers(searchQuery);

//System.out.println("response from search user method: 
"+searchResponse);

return new ModelAndView("user", "people", searchResponse);
}

于 2012-11-01T13:11:07.187 回答