2010-09-09 5 views
0

Мне нужно загрузить файл в strurts 2 и сохранить его на сервере, я использую googles без использования, может кто-нибудь дать лучшую идею. Заранее спасибоЗагрузка файла в strurts 2

ответ

0

Вот весь код struts2 файл загружен.

Действие файла (fileupload.java)

package com.tutorialspoint.struts2; 

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-6.0.33/work/"; 

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

index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="s" uri="/struts-tags"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
"http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<title>File Upload</title> 
</head> 
<body> 
    <form action="upload" method="post" enctype="multipart/form-data"> 
     <label for="myFile">Upload your file</label> 
     <input type="file" name="myFile" /> 
     <input type="submit" value="Upload"/> 
    </form> 
</body> 
</html> 

success.jsp

<%@ page contentType="text/html; charset=UTF-8" %> 
<%@ taglib prefix="s" uri="/struts-tags" %> 
<html> 
<head> 
<title>File Upload Success</title> 
</head> 
<body> 
You have successfully uploaded <s:property value="myFileFileName"/> 
</body> 
</html> 

Приветствия.