日期:2014-05-20  浏览次数:20903 次

struts2 中上传文件 问题
一、<s:a> 怎么设置弹出新对话框啊?也就是 _blank属性
二、拿来孙鑫的 Struts2 深入详解,抄了代码 运行时没报错,可是查看上传文件的文件夹中没有发现对应上传的文件,奇怪了! 我用的是MyEclipse7.0 。好奇怪啊。返回的页面上 fileFileName dir都有,不过fileContextType 显示不出来.

请高手帮忙啊!!!!

具体代码:

package tea;

import com.opensymphony.xwork2.ActionSupport;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
public class LoadPicAction extends ActionSupport {
private File file;
private String fileFileName;
private String fileContextType;
private String uploadDir;


public File getFile() {
return file;
}

public void setFile(File file) {
this.file = file;
}

public String getFileFileName() {
return fileFileName;
}

public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}

public String getFileContextType() {
return fileContextType;
}

public void setFileContextType(String fileContextType) {
this.fileContextType = fileContextType;
}

public String getUploadDir() {
return uploadDir;
}

public void setUploadDir(String uploadDir) {
this.uploadDir = uploadDir;
}

public void validate() {
if(fileFileName==null || fileFileName.equals("")){
this.addFieldError("form1", "请选择上传照片!");
}

}

public String execute() throws Exception {
this.setUploadDir("/tphoto");
String path=ServletActionContext.getServletContext().getRealPath(uploadDir);
File dir=new File(path);
if(!dir.exists())dir.mkdir();

BufferedOutputStream bos=null;
BufferedInputStream bis=null;
try{
FileInputStream fis=new FileInputStream(file);
bis=new BufferedInputStream(fis);

FileOutputStream fos=new FileOutputStream(new File(dir,this.getFileFileName()));
bos=new BufferedOutputStream(fos);

byte[] buf=new byte[4096];

int len=-1;
while((len=bis.read(buf))!=-1){
bos.write(buf, 0, len);
}

catch (IOException e) {
this.addFieldError("file", "上传照片失败!");
return INPUT;
}
finally{
try{
if(null!=bis)bis.close();
}
catch(IOException e){
e.printStackTrace();
}

try{
if(null!=bos)bos.close();
}
catch(IOException e){
e.printStackTrace();
}
}
return SUCCESS;
}




}


------解决方案--------------------
<filter>
<filter-name>struts-clearup</filter-name>
<filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
</filter>
<filter-mapping>
<filter-name>struts-clearup</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在web.xml中这个配置了没有
------解决方案--------------------
package com.action;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class UpLoadAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private String title;