首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

struts2 中上传文件 有关问题

2012-01-29 
struts2 中上传文件 问题一、s:a怎么设置弹出新对话框啊?也就是_blank属性二、拿来孙鑫的 Struts2 深入详

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;


private File upload;
private String uploadFileName;
private String uploadContentType;
private String savePath;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public File getUpload() {
return upload;
}

public void setUpload(File upload) {
this.upload = upload;
}

public String getUploadFileName() {
return uploadFileName;
}

public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}

public String getUploadContentType() {
return uploadContentType;
}

public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}

@SuppressWarnings("deprecation")
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}

public void setSavePath(String savePath) {
this.savePath = savePath;
}

@Override
public String execute() throws Exception {
try{
FileOutputStream fos = new FileOutputStream(getSavePath() + "\\"
+ getUploadFileName());
FileInputStream fis = new FileInputStream(getUpload());
BufferedInputStream bff = new BufferedInputStream(fis); 
byte[] buffer = new byte[1024*4];
int len = 0;
while ((len = bff.read(buffer)) > 0) {
System.out.println("len = "+len);
fos.write(buffer, 0, len);
}
}catch(IOException e){
e.printStackTrace();
}

return SUCCESS;
}

}
<action name="upload" class="com.action.UpLoadAction">
//上传的类型
<interceptor-ref name="fileUpload">
<param name="allowedTypes">
image/bmp,image/png,image/gif,image/jpg,image/pjpeg 
</param>
<param name="maximumSize">1024000</param>
</interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
//上传的路径
<param name="savePath">/upload</param>
<result>/uploadResult.jsp</result>
<result name="input">/error.jsp</result>
</action>
[解决办法]
我之前的问题是文件上传成功了,但是在WEB-INF目录的文件夹下没有找到上传的文件,后来我全盘搜索,终于找了。
因为用的是MyEclipse的内置tomcat,所以文件被上传在tomcat的那个地方了,也不知道你是否和我的相同,试试看吧
[解决办法]
在程序中打印:file、fileFileName、fileContextType看下结果,查看下jsp文件和web.xml是否有错误。
[解决办法]
应该是上传到Tomcat里面了。如果你在Eclipse里部署的话,就会这样。

热点排行