gpt4 book ai didi

java - 使用Struts2上传图片到数据库

转载 作者:搜寻专家 更新时间:2023-11-01 03:36:40 25 4
gpt4 key购买 nike

我正在尝试使用 Struts2 在数据库中上传图像(文件),但出现异常。我已经尝试过使用多种方式(文件,部分),但它仍然显示空指针异常。

索引.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;
}

网络.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)

请帮忙。

谢谢。

最佳答案

Action 类的一点改动解决了我的问题。这是代码Action类执行方法

 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;
}

关于java - 使用Struts2上传图片到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29658134/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com