2013-08-16 6 views
0

JSP 프로그램에서 파일을 업로드하기 위해 org.apache.commas.*, org.apache.commas.disk.*, org.apache.commas.fileupload.servlet.* 패키지를 사용하고 있으며 Struts가 필요 없으며이 데이터로 쉽게 작업 할 수 있었지만 Struts 2 코어 라이브러리를 추가했을 때 MyEclipse 8.5를 사용하는 웹 프로젝트에서 작동하지 않고 필드를 찾을 수 없습니다. 프로그램이 upload.parseRequest 일 수 있습니다. 은 솔직히 문제를 이해 할 수없는 나는, 그래서 나는 너무왜 필드가 발견되지 않는가

AddCategory.jsp 내 프로그램 공유 :

<html> 
<head> 
    <meta http-equiv="refresh" content="30"> 
</head> 

<script type="text/javascript"> 
    function blank() { 


    if (document.cate.cat.value == "Enter New Category") { 
     alert(" Category must not be blanked !!!"); 
     document.cate.cat.focus(); 
     return false; 
    } 
    else if (!document.getElementById("file1").value) { 
     alert("No file selected"); 
     return false; 
    } 
    else { 
     return true; 
    } 
    } 

</script> 
<form name="cate" action="CategoryAdded.jsp" method="post" enctype="multipart/form-data" onsubmit="return blank()"> 
    <table width="100%" border="0"> 
    <tr> 
     <th colspan="2" scope="col"> 
     <div align="center">Create New Category</div> 
     </th> 
    </tr> 
    <tr> 
     <td width="50%"> 
     <div align="right">Enter New Category:</div> 
     </td> 
     <td width="50%"> 
     <input name="cat" type="text" id="cat" value="Enter New Category" 
       onFocus="if(this.value== 'Enter New Category'){ this.value='' ; this.style.background='white';}" 
       onBlur="if(this.value==''){this.value='Enter New Category'; this.style.background='lightyellow'}"> 
     </td> 
    </tr> 

    <tr> 
     <td width="50%"> 
     <div align="right">Upload photo:</div> 
     </td> 
     <td width="50%"><input name="file1" type="file" id="file1"></td> 
    </tr> 


    <tr> 
     <td colspan="2"> 
     <div align="center"> 
      <input type="submit" name="Submit" value="Add Category"> 
     </div> 
     </td> 
    </tr> 
    </table> 
</form> 
</html> 

CategoryAdded.jsp :

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" %> 
<%@page import="java.io.*" %> 
<%@ page language="java" errorPage="" %> 
<%@ page import="java.sql.*" %> 
<%@ page import="org.apache.commons.io.*" %> 
<%@page import="java.util.Iterator,java.util.List" %> 
<%@page import="org.apache.commons.*,org.apache.commons.fileupload.disk.*,org.apache.commons.fileupload.servlet.*" %> 
<%@ page import="java.util.*" %> 
<%@page import="org.apache.commons.fileupload.FileItemFactory" %> 
<%@page import="org.apache.commons.fileupload.FileItem" %> 
<%@page import="org.apache.commons.fileupload.FileUploadException" %> 
<%@page import="p1.DBInfo" %> 
<%@page import="p1.Identy" %> 
<% 
    String path = request.getContextPath(); 
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; 
%> 
<html> 
<% 
    String pname = ""; 
    Identy id = new Identy(); 
    String cod = id.code(); 

    boolean isMultipartContent = ServletFileUpload.isMultipartContent(request); 
    if (!isMultipartContent) { 
    System.out.println("No multipart found"); 
    return; 
    } 


    FileItemFactory factory = new DiskFileItemFactory(); 
    ServletFileUpload upload = new ServletFileUpload(factory); 
    try { 
    List<FileItem> fields = upload.parseRequest(request); 
    Iterator<FileItem> it = fields.iterator(); 

    if (!it.hasNext()) { 
     System.out.println("No fields  found"); 
     return; 
    } 


    DBInfo obj = new DBInfo(); 
    Connection cn = obj.getConn(); 
    PreparedStatement ps = cn.prepareStatement("insert into category values(?,?,?)"); 

    while (it.hasNext()) { 

     FileItem fileItem = it.next(); 

     if (fileItem.getFieldName().equals("cat")) { 
     pname = fileItem.getString(); 
     System.out.println("category name is " + pname); 
     } 

     boolean isFormField = fileItem.isFormField(); 

     if (!isFormField) { 
     String s = fileItem.getName().substring(fileItem.getName().lastIndexOf("\\") + 1); 

     fileItem.write(new File("D:\\Practice\\ShoppingCart\\WebRoot\\images\\" + s)); 
     System.out.println(s); 
     fileItem.getOutputStream().close(); 

     ps.setString(3, "D:\\Practice\\ShoppingCart\\WebRoot\\images\\" + s); 
     } 
    } 

    ps.setString(1, pname); 
    ps.setString(2, pname + cod); 

    int i = ps.executeUpdate(); 

    if (i == 1) { 
%> 
<head> 


    <script type="text/javascript"> 
    function myFunction() { 


     var r = confirm("New Category Added Successfully!!!\nIf you Want to Add more New Category then Press Ok!!!"); 
     if (r == true) { 

     window.location = "AddCategory.jsp"; 


     } 
     else { 
     window.location = "Tryy.jsp"; 
     } 

    } 
    </script> 


</head> 

<body onload="myFunction()"> 
</body> 

<% 
    } 
    cn.close(); 

    } catch (Exception e) { 
    e.printStackTrace(); 
    } 
%> 
</html> 
+1

귀하의 질문을 이해할 수 없습니다. 어떤 필드가 발견되지 않았습니다. 오류는 무엇 이었습니까? 어떻게 될 것으로 예상됩니까? – gerrytan

+0

Struts2를 사용할 때 JSP에서이 파일 업로드 논리를 수행하는 이유는 무엇입니까? struts2가이를 투명하게 처리하도록 제안합니다. 또한 JSP에서 DB 작업을 수행하여 MVC 프레임 워크를 사용하는 전체 목적을 이겨내고 있습니다. –

+0

하지만 struts 2를 너무 많이 모릅니다 ..... 그리고 struts를 사용하여 파일을 업로드하는 방법 ... 기본적으로 스트럿츠 2를 사용하여 파일을 업로드하는 방법 – user2685033

답변

0

가장 좋은 것은이를 다시 작성하는 것입니다 JSP는 스크립틀릿을 제거하고 비즈니스 로직을 액션 클래스로 이동시킵니다.

콘텐츠를 조건부로 렌더링하려면 Struts2 <s:if><s:else> 태그를 사용할 수도 있습니다. commons-fileUpload

가 올바르게 struts-2-upload-multiple-files-example 같은 예 Struts2 프로젝트를 실행할 수 있습니다 그것을 사용하는, Struts2에 파일을 업로드에 대한 기본 구현입니다.

+0

을 다시 볼 수있다. 스트럿츠 2를 사용하는 방법을 안다. 나처럼 초보자에게는 매우 복잡하다.) 그런 다음 간단하게 파일을 업로드 할 수있는 방법은 무엇인가? – user2685033

관련 문제