sea_moon40 2009-01-25 13:40
浏览 257
已采纳

关于struts2上次图片和html以外的文件问题

今天看了struts2权威指南 第六章上传文件

发现在上传gif或html等文件时候

action中的
uploadFileName
uploadContentType
是可以显示出来的。
一旦上传doc.xls.,ppt等就显示为null
不知道怎么解决,谢谢大家帮忙.

以下是struts2 配置


image/*,text/*,application/msword,application/ms-excel
/files
/succ.jsp
/file.jsp

以下是action
package struts2.test.action;

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.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UploadAction extends ActionSupport{

private static final long serialVersionUID = 1L;
private File upload;
private String uploadContentType;
private String savePath;
private String uploadFileName;
private String allowTypes;

@SuppressWarnings("deprecation")
public String getSavePath() {
    return ServletActionContext.getServletContext().getRealPath(savePath);
}
public void setSavePath(String savePath) {
    this.savePath = savePath;
}
public File getUpload() {
    return upload;
}
public void setUpload(File upload) {
    this.upload = upload;
}
public String getUploadContentType() {
    return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
    this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
    return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
    this.uploadFileName = upload.getName();
}
public String execute() throws IOException{
    String filterResult = filterType(getAllowTypes().split(","));
    if(filterResult != null){
        ActionContext.getContext().put("typeError", "您要上传的文件类型不正确");
        return filterResult;
    }else{
            FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
            FileInputStream fis = new FileInputStream(getUpload());
            byte[] buffer = new byte[1024];
            int len = 0;
            while((len = fis.read(buffer))>0){
                fos.write(buffer,0,len);
            }
            fos.close();
            fis.close();
            return SUCCESS;
        }
}
public String getAllowTypes() {
    return allowTypes;
}
public void setAllowTypes(String allowTypes) {
    this.allowTypes = allowTypes;
}

public String filterType(String[] types){
    System.out.println(getUploadContentType());
    String fileType = getUploadContentType();
    for(String type:types){
        if(type.equals(fileType)){
            return null;
        }
    }
    return INPUT;
}

}

jsp 页面

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@taglib prefix="s" uri ="/struts-tags"%>


文件上传


${typeError}









  • 写回答

1条回答 默认 最新

  • iteye_2372 2009-01-26 22:21
    关注

    word,excel,flash应该是这几个类型:
    1.application/vnd.ms-excel
    2.application/vnd.ms-word
    3.application/x-shockwave-flash

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助