我正在尝试使用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>
行动类
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:\ Users \用户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)
请帮忙。
感谢。
答案 0 :(得分:1)
Action类的一点变化解决了我的问题。 这是守则 Action Class执行方法
public String execute()
{
try
{
FileInputStream fis = new FileInputStream(img);
//ip = new byte[fis.available()];
//System.out.print("printfis"+fis.available());
//fis.read(ip);
//fis.close();
//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.setBinaryStream(1, fis, (int)img.length());
int i = stmt.executeUpdate();
if(i>0)
{
return SUCCESS;
}
}
catch(Exception e)
{
e.printStackTrace();
}
return SUCCESS;
}