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

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法