使用JSP不会将图像上载到目录中

时间:2015-10-18 16:46:50

标签: html jsp

我只是想在JSP中使用表单上传图像,因为我编写了一个代码,按照下面的代码,它不是插入while循环。结果图像未上传到目录中。

你能帮忙解决问题吗?

html代码

<html>
 <head><title>Upload page</title></head></p> <p><body>
 <form action="upload_jsp.jsp" method="post" enctype="multipart/form-data" name="form1" id="form1">
   <center>
   <table border="2">
       <tr>
           <td align="center"><b>Multipale file Uploade</td>
       </tr>
       <tr>
           <td>
               Specify file: <input name="file" type="file" id="file">
           <td>
       </tr>
       <tr>
          <td>
             Specify file:<input name="file" type="file" id="file">
          </td>
        <tr>
           <td>
              Specify file:<input name="file" type="file" id="file">
           </td>
         </tr>
         <tr>
            <td align="center">
               <input type="submit" name="Submit" value="Submit files"/>
            </td>
         </tr>
    </table>
    <center>
 </form>
 </body>
 </html>

jsp代码

<%@ page import="java.util.List"%>  
<%@ page import="java.util.Iterator"%>  
<%@ page import="java.util.ResourceBundle" %>  
<%@ page import="java.io.File"%>  
<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>  
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>  
<%@ page import="org.apache.commons.fileupload.*"%>  

<%           
            String path= "C:\\Users\\gur29175\\Desktop"  ;          
boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
if (!isMultipart) {  
} else {  
     String directory="";  
            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(path+itemName);     
              //File savedFile = new File("C:\\Users\\sagar\\Desktop\\JAVA Training\\code(1)\\test\\WebContent\\WEB-INF\\uploads\\"+itemName);      
             //File savedFile = new File(config.getServletContext ().getRealPath("/")+"uploadedFiles/"+itemName);  
                           item.write(savedFile);    

//out.println("<tr><td><b>Your file has been saved at the loaction:</b></td></tr><tr><td><b>"+config.getServletContext().getRealPath  
//("/")+"uploadedFiles"+"\\"+itemName+"</td></tr>");  
out.println("<tr><td><b>Your file has been saved at the loaction:</b></td></tr><tr><td><b>"+path+  
("/")+"uploadedFiles"+"\\"+itemName+"</td></tr>");  
                   } catch (Exception e) {  
                           e.printStackTrace();  
                   }  
           }  
           }  
   }  
   %>  

1 个答案:

答案 0 :(得分:0)

这是可能有问题的代码行,因为item.getName()将返回源文件所在的完整路径。这可能会导致格式错误的文件路径。

尝试更换:

String itemName = item.getName();

使用:

String itemName = FilenameUtils.getName(item.getName());

确保导入了FilenameUtils:

<%@ page import="org.apache.commons.io.FilenameUtils" %>

您的网络应用程序库中有 commons-io.jar