简体   繁体   English

从jsp上传文件

[英]Upload file from jsp

I am trying to upload multiple files from jsp using below code: 我正在尝试使用以下代码从jsp上传多个文件:

When I execute it from my local machine I am able to upload in the local systems folder. 当我从本地计算机执行它时,我可以在本地系统文件夹中上载。 But when I access the same from remote machine I am expecting that the files should be uploaded to the same machine where my tomcat exist ,but I get error C:\\Files\\`folder/file not found`. 但是,当我从远程计算机访问同一文件时,我期望文件应该上传到我的tomcat存在的同一台计算机上,但是会出现错误C:\\ Files \\`folder / file not found`。 Please guide.How to upload it in remote machine or where the tomcat resides 请指导。如何将其上传到远程计算机或tomcat所在的位置

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
     if (!isMultipart) {
     } else {
               FileItemFactory factory = new DiskFileItemFactory();
               ServletFileUpload upload = new ServletFileUpload(factory);
               List items = null;
               try {
                       items = upload.parseRequest(request);
               } catch (FileUploadException e) {
                       e.printStackTrace();
               }
               Iterator itr = items.iterator();
               while (itr.hasNext()) {
               FileItem item = (FileItem) itr.next();
               if (item.isFormField()) {
               } else {
                       try {
                               String itemName = item.getName();
                               File savedFile = new File("C:\\Files\\a.tiff");
                               item.write(savedFile);  

                       } catch (Exception e) {
                               e.printStackTrace();
                       }
               }
               }
       }

This is the path where I want to upload all files C:\\\\Files\\\\ of the machine where tomcat is. 这是我要上载tomcat所在机器的所有文件C:\\\\Files\\\\的路径。

Change your file save path to new File("C:\\\\Files\\\\"); 将文件保存路径更改为new File("C:\\\\Files\\\\"); . Even, still you have any problem, then create one folder with Files name in another drive E or F whatever and change your code like new File("E:\\\\Files\\\\"); 即使您仍然有任何问题,也可以在另一个驱动器EF中用Files名创建一个文件夹,然后更改代码,例如new File("E:\\\\Files\\\\"); if you wanna save your file to E drive. 如果您想将文件保存到E盘。

Note: Since, C drive is the primary drive in windows OS, so due to lack of permission, it won't allow to create new file/folder in that drive. 注意:由于C驱动器是Windows OS中的主驱动器,因此由于缺少权限,因此不允许在该驱动器中创建新的文件/文件夹。 So, please try the alternative solution. 因此,请尝试其他解决方案。 I mean try to change your file location. 我的意思是尝试更改您的文件位置。

you need to change this new File("C:\\\\Files\\\\"); 您需要更改此new File("C:\\\\Files\\\\"); to a foldername on my your remote-server 到我的远程服务器上的文件夹名称

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

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