简体   繁体   中英

why doesn't my jspSmartUpload code work?

I want to implement an upload component in my servlet file,but it doesn't work. The code episode seems like follows:

SmartUpload smartUpload=new SmartUpload();
StringBuffer stringBuffer=new StringBuffer();
smartUpload.initialize(config,request, response);
try {
     smartUpload.upload();
 File file=smartUpload.getFiles().getFile(0);
 stringBuffer.append(file.getFileName());
 file.saveAs("/upload_resources/"+stringBuffer);
} catch (SmartUploadException e) {
 e.printStackTrace();
}

The upload_resource directory is just under the WebRoot directory,the code runs without any errors ,but the file is just not uploaded. By the way , even I changed the line

file.saveAs("/upload_resources/"+stringBuffer);

to

file.saveAs(request.getRealPath("/upload_resources/")+"/"+stringBuffer);

that is to use an absolute path, the file is not uploaded. Any help will be appreciate. Thanks.

I've never really worked with SmartUpload, but I can tell that you shouldn't be saving uploaded files in the webapp's deploy folder. They may all get lost whenever the webapp get redeployed with the simple reason that the uploaded files are not contained in the original WAR file. So you should not prepare the upload folder in the webapp's deploy folder, but on a fixed path outside the deploy folder.

If SmartUpload is well designed, I'd expect that

file.saveAs("/upload_resources/"+stringBuffer);

will save it to the /uploaded_resources folder on the root of the same disk as where the webserver is started from. So in for example Windows that would be C:\\uploaded_resources . Prepare and use that folder instead.

Further there's another potential problem when you're using the MSIE browser. This browser namely incorrectly includes the full client side path in the filename. I'm not sure if SmartUpload handles this properly, but you might want to debug the actual value of file.getFileName() and make sure that it's really only the filename in the form of filename.ext . Otherwise, you'd need to use String#substring() to substring the part after the last / and \\ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM