4
我想使用Struts2上傳圖像(文件)到數據庫中,並且發生異常。我已經嘗試過使用多種方式(文件,部分),但它仍然顯示空指針異常。使用Struts2在數據庫中上傳圖像
的index.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:form action="imgup" method="post" enctype="multipart/form-data">
<s:file name="img" ></s:file>
<s:submit value="upload"></s:submit>
</s:form>
</body>
</html>
struts.xml中
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<action name="imgup" class="com.stru.imgupload">
<interceptor-ref name="fileUpload">
<param name="maximumSize">2097152</param>
<param name="allowedTypes">
image/png,image/gif,image/jpeg,image/pjpeg
</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
<result name="success">index.jsp</result>
</action>
</package>
</struts>
Action類
package com.stru;
import java.io.*;
import java.sql.*;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class imgupload extends ActionSupport implements ServletRequestAware{
/**
*
*/
private static final long serialVersionUID = 1L;
private HttpServletRequest request;
private File img;
private byte[] ip;
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public byte[] getIp() {
return ip;
}
public void setIp(byte[] ip) {
this.ip = ip;
}
private String imagecontenttype;
private String filename;
public File getImg() {
return img;
}
public void setImg(File img) {
this.img = img;
}
public String getImagecontenttype() {
return imagecontenttype;
}
public void setImagecontenttype(String imagecontenttype) {
this.imagecontenttype = imagecontenttype;
}
@Override
public void setServletRequest(HttpServletRequest request) {
this.request=request;
}
public String execute()
{
try
{
String filepath=request.getSession().getServletContext().getRealPath("/");
System.out.print("path"+filepath);
File filetocreate = new File(filepath, filename);
System.out.print("tocreate"+filetocreate.getName());
FileUtils.copyFile(img, filetocreate);
ip = getBytes(filetocreate);
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/mdb","root","tiger");
PreparedStatement stmt = con.prepareStatement("insert into strpic(pics) values(?)");
stmt.setBytes(1, ip);
int i = stmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
}
return SUCCESS;
}
}
的web.xml
<display-name>strutsimage</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
控制檯: 路徑C:\用戶\ RAVI \ mca_1.metadata.plugins \ org.eclipse.wst.server.core \ TMP0 \ wtpwebapps \ strutsimagejava.lang .NullPointerException 在java.io.File中。(File.java:317) 在com.stru.imgupload.execute(imgupload.java:100)
請幫助。
謝謝。
你能張貼您收到,所以我們可以看到,受影響的線路異常文本?謝謝 – TejjD
你只用一個攔截器爲你採取行動。使用默認堆棧並在其中配置'fileUpload'攔截器。 –
感謝所有我的問題解決了... 通過簡單地創建一個對象 FileInputStream fis = FileInputStream(img);jdbc:insert ps.setBinaryStream(1,fis,(int)img.length()); –