我在谷歌和本网站上阅读了很多解决方案,但是没有任何帮助。 我正在尝试使用 randomUUID 在spring mvc project目录 \ webapp \ assets \ images 上传图片并上传到服务器并获取上述错误。 请别人帮助我
这是我的豆子
@Entity
@Table(name="documentsww")
public class Document {
@Id
@GeneratedValue
@Column(name="id")
private Integer id;
public Document(){
this.code="DOC"+UUID.randomUUID().toString().substring(26).toUpperCase();
this.code="DOC"+UUID.randomUUID().toString().substring(26).toUpperCase();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name="name")
private String name;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
@Column
private String code;
@Column(name="description")
private String description;
@Column(name="content")
@Transient
private MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
和Controller是
@Controller
public class ImageCoontroller {
@Autowired
DocumentDao documentDao;
@RequestMapping(value="/")
public String getpage() {
return "register";
}
@RequestMapping(value="/it")
public String save(
@ModelAttribute("document") Document document,HttpServletRequest request) {
System.out.println("the values are Description is Code is ");
documentDao.save(document);
if(!document.getFile().getOriginalFilename().equals("")){
ImageUtill.uploadfile(request,document.getFile(),document.getCode());
}
return "register";
}
}
imageUtill class
public class ImageUtill {
private static final String ABS_PATH="C:\\Users\\Junaid\\Documents\\workspace-sts-3.9.0.RELEASE\\Uplaod\\src\\main\\webapp\\assets\\images";
private static String REAL_PATH="";
public static void uploadfile(HttpServletRequest request, MultipartFile file, String code) {
Logger loger=LoggerFactory.getLogger(ImageUtill.class);
REAL_PATH=request.getSession().getServletContext().getRealPath("/assets/images/");
loger.info(ABS_PATH);
loger.info(REAL_PATH);
System.out.println(REAL_PATH);
System.out.println(ABS_PATH);
// TODO Auto-generated method stub
if(!new File(ABS_PATH).exists()){
new File(ABS_PATH).mkdirs();
}
if(!new File(REAL_PATH).exists()){
new File(REAL_PATH).mkdirs();
// F:\SpringProjects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Image\WEB-INF\images
//F:\SpringProjects\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\Image\WEB_INF\images
}
try {
//
file.transferTo(new File(REAL_PATH+code+".jpg"));
file.transferTo(new File(ABS_PATH+code+".jpg"));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
}
答案 0 :(得分:0)
你不能两次使用MultipartFile #transferTo方法,第二次调用transferTo方法应该用文件复制的逻辑替换。您可以使用this method复制文件。
几乎伪代码:
String firstFilePath = REAL_PATH+code+".jpg";
String secondFilePath = ABS_PATH+code+".jpg";
file.transferTo(new File(firstFilePath));
Files.copy(Paths.get(firstFilePath), Paths.get(secondFilePath));