gpt4 book ai didi

带有 Mongodb 的 Java Spring 用户图像 uploader

转载 作者:行者123 更新时间:2023-11-29 09:09:25 25 4
gpt4 key购买 nike

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

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

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

1.如何动态创建这些文件夹以及位置。我读过该文件夹通常会存储在 WEB-INF 中。

或者图像应该/可以作为 blob 文件存储在 mongodb 中吗?

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

我试过从这里改编代码 http://www.roseindia.net/tutorial/spring/spring3/web/spring-3-mvc-fileupload-example.html

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


FileUpload.java



<pre><code>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;
}
}
</code></pre>


FileUploadController.java


package net.fileupload;



<pre><code> 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);
}
}
</code></pre>


FileUploadValidator.java


package net.fileupload;



<pre><code> 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 ");
}
}
}
</code></pre>

检索图像。

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


画廊 Controller


import java.util.ArrayList;



<pre><code> 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;
}

}
</code></pre>


监听器 Controller

<br/>
/*
* Gallery methods
*/



<pre><code> @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);
}
</code></pre>

最佳答案

我当前的用户 Controller 看起来像这样。

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

我当前的映射可以很好地使用 id 作为参数。 localhost:8080/user?id=1312321321



/*
* 用户
*/
@RequestMapping(method=RequestMethod.GET, value={"/user","/user/{id}"})
public ModelAndView profileDisplay(
HttpServletRequest请求,
HttpServletResponse 响应,
@RequestParam(value="id", required=false) 字符串id
) 抛出 UnknownHostException, MongoException {
ServiceSerlvet.appendSession(请求);
//获取搜索所有用户
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put("_id", new ObjectId(id));
列表 searchResponse =
PersonController.searchUsers(searchQuery);

//System.out.println("搜索用户方法的响应:
“+搜索响应);

return new ModelAndView("用户", "人", searchResponse);
}

关于带有 Mongodb 的 Java Spring 用户图像 uploader ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13177762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com