如何使用struts2上传文件并将路径存储在数据库中

时间:2013-03-18 09:35:32

标签: jsp struts2

我一直在努力完成这项任务但却无法成功。我可以上传可以存储在“C:\ apache-tomcat-6.0.33 \ work”中的文件,但我不知道如何在数据库中存储路径。这是我的代码。

package com.rajesh.action;

import java.io.File;
import org.apache.commons.io.FileUtils;
import java.io.IOException; 

import com.opensymphony.xwork2.ActionSupport;

public class UploadFile extends ActionSupport{
private File myFile;
private String myFileContentType;
private String myFileFileName;
private String destPath;

public String execute()
{
/* Copy file to a safe location */
destPath = "C:/apache-tomcat-7.0.37/upload";

try{
System.out.println("Src File name: " + myFile);
System.out.println("Dst File name: " + myFileFileName);

File destFile  = new File(destPath, myFileFileName);
FileUtils.copyFile(myFile, destFile);

}catch(IOException e){
e.printStackTrace();
return ERROR;
}

return SUCCESS;
}

public File getMyFile() {
  return myFile;
}
public void setMyFile(File myFile) {
  this.myFile = myFile;
}
public String getMyFileContentType() {
  return myFileContentType;
}
public void setMyFileContentType(String myFileContentType) {
  this.myFileContentType = myFileContentType;
}
public String getMyFileFileName() {
  return myFileFileName;
}
public void setMyFileFileName(String myFileFileName) {
  this.myFileFileName = myFileFileName;
}
}

这是我的jsp上传文件。

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>Struts 2 - Login Application | ViralPatel.net</title>
</head>

<body>
<h2>Struts 2 - Login Application</h2>
<s:actionerror />
<s:form action="login" method="post">
<s:textfield name="username" key="label.username" size="20" />
<s:password name="password" key="label.password" size="20" />
<s:submit method="execute" key="label.login" align="center" />
</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>
<constant name="struts.enable.DynamicMethodInvocation"
value="false" />
<constant name="struts.multipart.maxSize" value="1000000" />
<constant name="struts.devMode" value="false" />
<constant name="struts.custom.i18n.resources"
value="ApplicationResources" />

<package name="default" extends="struts-default" namespace="/">
<action name="login" class="com.rajesh.action.LoginAction">
<result name="success">admin.jsp</result>
<result name="error">loginstruts.jsp</result>
</action>
<action name="upload" class="com.rajesh.action.UploadFile">
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
</struts>

请帮帮我,我是新struts2,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

将文件上传到目标路径后,在您的操作中编写数据库逻辑,或者您可以创建DAO类来连接数据库并将记录插入其中并在struts操作中使用该类。您需要在此行之后放置数据库连接代码你的代码..

FileUtils.copyFile(myFile,destFile);

示例代码:

//To be write after above line in your action class
String filePathToStoreInDB = destPath+"\\"+myFileFileName;
MyDAO myDAO = new MyDAO();
String result = myDAO.insertRecord(filePathToStoreInDB);
if(result.equals("success")){
//proceed your logic
}else{
//your logic to display errro message
}

//logic in DAO class to insert record
public class MyDAO{
//Your code to insert record in database
return "success"; //if record inserted successfully
retrun "fail"; //if record failed to insert
}
}

注意:您可以在数据库中使用列类型,因为Varchar2或Varchar取决于您的要求。