简体   繁体   English

为什么我的jspSmartUpload代码不起作用?

[英]why doesn't my jspSmartUpload code work?

I want to implement an upload component in my servlet file,but it doesn't work. 我想在我的servlet文件中实现一个上载组件,但是它不起作用。 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. upload_resource目录位于WebRoot目录下,代码运行无任何错误,但文件未上传。 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. 我从未真正使用过SmartUpload,但是我可以告诉您不要将上传的文件保存在webapp的deploy文件夹中。 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. 每当重新部署Web应用程序时,它们可能都会丢失,原因很简单,即原始的WAR文件中不包含上载的文件。 So you should not prepare the upload folder in the webapp's deploy folder, but on a fixed path outside the deploy folder. 因此,您不应在Webapp的deploy文件夹中准备上载文件夹,而应在deploy文件夹外的固定路径上准备。

If SmartUpload is well designed, I'd expect that 如果SmartUpload设计合理,我希望

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. 会将其保存到与启动Web服务器所在磁盘相同的根目录下的/uploaded_resources文件夹中。 So in for example Windows that would be C:\\uploaded_resources . 因此,例如在Windows中,它将为C:\\uploaded_resources Prepare and use that folder instead. 准备并使用该文件夹。

Further there's another potential problem when you're using the MSIE browser. 此外,当您使用MSIE浏览器时,还有另一个潜在的问题。 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 . 我不确定SmartUpload是否可以正确处理此问题,但是您可能想调试file.getFileName()的实际值,并确保它实际上只是filename.ext形式的filename.ext Otherwise, you'd need to use String#substring() to substring the part after the last / and \\ . 否则,您需要使用String#substring()对最后一个/\\之后的部分进行子字符串化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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