我将图片插入数据库

时间:2016-01-12 08:10:27

标签: jsp

org.apache.jasper.JasperException:无法为JSP编译类:

  

jsp文件中的第52行:/insertimage.jsp发生错误   令牌上的语法错误,删除这些令牌   49:试试   50:{   51:itemName = item.getName();   52:File savedFile = new File(“config.getServletContext()。getRealPath(”/“)+”Example \ image-folder \“+ itemName);   53:item.write(savedFile);   54:}   55:catch(例外e)

     

jsp文件中的第52行:/insertimage.jsp发生错误   图像无法解析为变量   49:试试   50:{   51:itemName = item.getName();   52:File savedFile = new File(“config.getServletContext()。getRealPath(”/“)+”Example \ image-folder \“+ itemName);   53:item.write(savedFile);   54:}   55:catch(例外e)

<%@ page import="org.apache.commons.fileupload.servlet.ServletFileUpload" %> 
<%@ page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%> 
<%@ page import="org.apache.commons.fileupload.*"%> 
<%@ page import="java.util.*, java.io.*" %> 
<%@ page import="java.util.Iterator"%> 
<%@ page import="java.util.List"%> 
<%@ page import="java.io.File"%> 
<%@ include file="conn.jsp"%> <!-- to connect a database--> 

<% 
try 
{ 
String ImageFile=""; 
String itemName = ""; 
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.getMessage(); 
} 

Iterator itr = items.iterator(); 
while (itr.hasNext()) 
{ 
FileItem item = (FileItem) itr.next(); 
if (item.isFormField()) 
{ 
String name = item.getFieldName(); 
String value = item.getString(); 
if(name.equals("ImageFile")) 
{ 
ImageFile=value; 
} 

} 
else 
{ 
try 
{ 
itemName = item.getName(); 
File savedFile = new File("config.getServletContext().getRealPath("/")+"Example\\image-folder\\"+itemName); 
    item.write(savedFile); 
    } 
    catch (Exception e) 
    { 
    out.println("Error"+e.getMessage()); 
    } 
    } 
    } 
    try 
    { 
    st.executeUpdate("insert into test values ('"+itemName+"')");     
    } 
    catch(Exception el) 
    { 
    out.println("Inserting error"+el.getMessage()); 
    } 
    } 
    } 
    catch (Exception e){ 
    out.println(e.getMessage()); 
    } 
    %> 

1 个答案:

答案 0 :(得分:0)

有一个不必要的双引号。要修复,请替换

File savedFile = new File("config.getServletContext().getRealPath("/")+"Example\\image-folder\\"+itemName); 

使用

File savedFile = new File(config.getServletContext().getRealPath("/")+"Example\\image-folder\\"+itemName);