java - spring uploading files issue -
java - spring uploading files issue -
i'm trying utilize multiple files uploadind upload images, , want store 3 copies of images (origin, thumbnail , mini format). problem happens after commentet //save thumbnail
. got exception java.lang.illegalstateexception: file has been moved - cannot transferred again.
how can prepare it?
@requestmapping(value = "/uploadfiles", method = requestmethod.post) public string uploadfiles( @modelattribute("uploadform") donwloadfiles uploadform, @modelattribute("id") int id, modelmap map) throws illegalstateexception, ioexception { properties properties = new properties(); string savedirectory = "/home/desktop/photos/"; list<multipartfile> files = uploadform.getfiles(); list<string> filenames = new arraylist<string>(); if (null != files && files.size() > 0) { (multipartfile multipartfile : files) { photo photo = new photo(); string filename = string.valueof(system.currenttimemillis()); photo.setshop(shopservice.findbyid(id)); photo.setname(filename); photo.setpath(savedirectory + filename); photoservice.addphoto(photo); if (!"".equalsignorecase(filename)) { //save origin image multipartfile.transferto(new file(savedirectory + filename)); //save thumbnail multipartfile.transferto(new file(savedirectory + "thumbnail_" + filename)); //save mini multipartfile.transferto(new file(savedirectory + "mini_" + filename)); } } message = "successfully uploaded"; } map.put("message", message); homecoming "/edit/" + id; }
from spring javadoc :
void transferto(file dest) throws ioexception, illegalstateexception
transfer received file given destination file. may either move file in filesystem, re-create file in filesystem, or save memory-held contents destination file. if destination file exists, deleted first.
if file has been moved in filesystem, operation cannot invoked again. therefore, phone call method 1 time able work storage mechanism.
(emphasis mine)
you cannot invoke transferto()
twice on same file.
but saved in savedirectory + filename
, nil stops copying file or want (by example, fileutils.copy()
commons-io)
java spring
Comments
Post a Comment