我必须上传文件并将文件名,文件保存在系统中的路径和文件类型(.pdf,.txt等)保存到oracle数据库中。我已经完成了文件上传路径,还编写了用于保存在数据库中的代码,但是值没有保存在数据库中。我是struts2的新手,所以请任何人都可以提供帮助..
的index.jsp:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>
<s:actionerror />
<s:form action="uploadAction" method="POST" enctype="multipart/form-data">
<s:file name="uploadFile" label="Choose File" size="40" />
<s:submit value="Upload" name="submit" />
</s:form>
的success.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<body>
File uploaded successfully...!!!
File Name : <s:property value="uploadFileFileName"/> <br>
Content Type : <s:property value="uploadFileContentType"/> <br>
Temp File Name : <s:property value="uploadFile"/>
</body>
</html>
动作类:
package java4s;
import java.io.File;
import java.sql.*;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ActionSupport;
public class LogingEx extends ActionSupport{
private static final long serialVersionUID = 1L;
private File uploadFile;
private String uploadFileContentType;
private String uploadFileFileName;
public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public String getUploadFileContentType() {
return uploadFileContentType;
}
public void setUploadFileContentType(String uploadFileContentType) {
this.uploadFileContentType = uploadFileContentType;
}
public String getUploadFileFileName() {
return uploadFileFileName;
}
public void setUploadFileFileName(String uploadFileFileName) {
this.uploadFileFileName = uploadFileFileName;
}
public String execute()
{
try{
String filePath = "c:/Myuploads"; // Path where uploaded file will be stored
System.out.println("Server path:" + filePath); // check your path in console
File fileToCreate = new File(filePath, uploadFileFileName);// Create file name same as original
FileUtils.copyFile(uploadFile, fileToCreate); // Just copy temp file content tos this file
System.out.println("inside database");
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@10.10.1.62:1521:mcrtest","SULTAN","SULTAN");
PreparedStatement statement = con.prepareStatement("insert into filedatabase " +"(name, type, path)" + "values(?,?,?)");
statement.setString(1,uploadFileFileName);
statement.setString(2,uploadFileContentType);
statement.setString(3,uploadFile.toString());
System.out.println("After database");
statement.close();
con.close();
}catch(Exception e)
{
e.printStackTrace();
addActionError(e.getMessage());
return INPUT;
}
return SUCCESS;
}
}
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<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>
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>
<include file="struts-default.xml"/>
<package name="a" extends="struts-default">
<action name="uploadAction" class="java4s.LogingEx">
<interceptor-ref name="exception" />
<interceptor-ref name="alias" />
<interceptor-ref name="servletConfig" />
<interceptor-ref name="prepare" />
<interceptor-ref name="i18n" />
<interceptor-ref name="chain" />
<interceptor-ref name="debugging" />
<interceptor-ref name="profiling" />
<interceptor-ref name="scopedModelDriven" />
<interceptor-ref name="modelDriven" />
<interceptor-ref name="fileUpload">
<param name="maximumSize">102400</param>
<param name="allowedExtensions"> .doc,.docx,.ppt,.pptx,.txt,.pdf,.zip </param>
</interceptor-ref>
<interceptor-ref name="checkbox" />
<interceptor-ref name="staticParams" />
<interceptor-ref name="actionMappingParams" />
<interceptor-ref name="params">
<param name="excludeParams"> dojo\..*,^struts\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError" />
<interceptor-ref name="validation">
<param name="excludeMethods"> input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods"> input,back,cancel,browse</param>
</interceptor-ref>
<result name="success">/success.jsp</result>
<result name="input">/index.jsp</result>
</action>
</package>
</struts>