2013-03-18 9 views
0

이 작업을 수행하려고했지만 성공하지 못했습니다. "C : \ apache-tomcat-6.0.33 \ work"에 저장할 수있는 파일을 업로드 할 수 있지만 데이터베이스에 경로를 저장하는 방법을 모르겠습니다. 여기 내 코드가있다.struts2를 사용하여 파일을 업로드하고 데이터베이스에 경로를 저장하는 방법

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> 

여기가 내가이 struts2 새로운 오전, 나 좀 도와주세요 내 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> 

이다, 어떤 도움을 크게 감상 할 수있다.

+0

DAO 계층에서 사용하고있는 프레임 워크는 무엇입니까? Hibernate 또는 JPA –

+0

jdbc를 사용 중이고 최대 절전 모드 또는 JPA를 사용하지 않습니다. 액션은 struts2에 의해 제어됩니다. –

+0

@RajeshAcharya 그렇다면, struts2가 동작을 제어 할 때 JDBC를 어떻게 사용하고 싶습니까? –

답변

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이 귀하의 요구 사항에 따라 달라집니다 당신이 데이터베이스에 열 형식을 취할 수 있습니다.

관련 문제