`

EXT+Struct2文件上传出现异常

阅读更多
Action类代码为
public String readexcel() throws Exception {
		
		String path = getRealyPath(uploaddir);
		File dir = new File(path);
		if (!dir.exists()) {
			dir.mkdir();
		}
		int length = excelfileFileName.length();
		int start = excelfileFileName.indexOf('.');
		if (!excelfileFileName.substring(start+1, length).equalsIgnoreCase("xls")) {
			//String msg = "{\"success\":false,\"file\":'wrongfileaaa文件错误'}";
			String a = "aaa文件错误";
			String msg = "{\"success\":true,\"msgs\":'" +a+ "'}";
			getResponse().setCharacterEncoding("UTF-8");
			//getResponse().setContentType("html/text");
			getResponse().getWriter().write(msg);
			return null;
		}else{
			BufferedInputStream bis = null;
			try {
				FileInputStream fis = new FileInputStream(excelfile);
				bis = new BufferedInputStream(fis);
				Workbook book = Workbook.getWorkbook(bis);
				Sheet sheet = book.getSheet(0);
				int cols = sheet.getColumns();
				int rows = sheet.getRows();
				List<String[]> list = new ArrayList<String[]>();
				for (int j = 0; j < rows; j++) {
					String [] strs = new String[cols];
					for (int i = 0; i < cols; i++) {
						strs[i] = sheet.getCell(i,j).getContents();
					}
					list.add(strs);
				}
				StaffBindDao staff = new StaffBindDao();
				//staff.addinfo(tablename, list);
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				try {
					if (bis!=null) {
						bis.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			System.out.println("sssssssssssssssssss");
			String msg = "{success:true, file:'a"+excelfileFileName+"!'}";
			getResponse().getWriter().write(msg);
			return null;
		}
	}


js代码为
Ext.BLANK_IMAGE_URL = "/abcd/ext/resources/images/s.gif";
Ext.form.Field.prototype.msgTarget = 'side';
Ext.QuickTips.init();
Ext.onReady(function(){
	var msg = function(title, msg){
        Ext.Msg.show({
            title: title, 
            msg: msg,
            minWidth: 200,
            modal: true,
            icon: Ext.Msg.INFO,
            buttons: Ext.Msg.OK
        });
    };
	var _form = new Ext.FormPanel({
        fileUpload: true,
        width: 485,
        height:285,
        frame: true,
        autoHeight: true,
        bodyStyle: 'padding: 10px 10px 0 10px;',
        labelWidth: 50,
        defaults: {
            anchor: '95%',
            allowBlank: false,
            msgTarget: 'side'
        },
        items: [{
            xtype: 'textfield',
            emptyText: '请填写要插入数据表名称',
            blankText: '数据表名称为必填写项',
            fieldLabel: '数据表',
            name:'tablename'
        },{
            xtype: 'fileuploadfield',
            id: 'form-file',
            emptyText: '请选择文件',
            blankText: '必须上传文件',
            fieldLabel: '文件',
            name: 'excelfile',
            buttonCfg: {
                text: '',
                iconCls: 'upload-icon'
            }
        }],
        buttons: [{
            text: '上传',
            handler: function(){
                if(_form.getForm().isValid()){
	                _form.getForm().submit({
	                	url: 'back/ReadExcelaction_readexcel.action',
	                    //url:'result.jsp',
	                    waitMsg: '文件正在上传',
	                    success: function(_form, o){
	                        msg('系统提示', '文件内容已被成功加载'+o.result.file);
	                    },
	                    failure:function(_form,o){
	                    	msg('系统提示', '文件内容已被成功加载'+o.result.file);
	                    	if(o.result.file=='wrongfile')
	                    	{
	                    		 msg('系统提示', '文件类型不正确!');
	                    		 return;
	                    	}
	                    }
	                });
                }
            }
        },{
            text: '取消',
            handler: function(){
                _form.getForm().reset();
            }
        }]
    });
	function test(){
		myform.form.submit();
	}
	var window = new Ext.Window({
		title:'Excel 文件的上传',
		width:500,
		height:143,
		items:[_form]
	});
	window.show();
});


XML配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	
	<package name="back" extends="struts-default" namespace="/back">
		
		<action name="*action_*" class="com.jws.action.{1}Action" method="{2}">
			<!-- 文件上传 拦截器-->
			<!-- 
				<interceptor-ref name="defaultStack">
				<param name="fileUpload.maximumSize">102400000</param>
				
				</interceptor-ref>
				
				
				<result name="test">index.jsp</result>
				 -->
				<param name="uploaddir">/WEB-INF/UploadFiles</param>
				<result name="test">../index.jsp</result>
				
		</action>
	</package>

</struts>


文件成功上传后
发现一个奇怪的问题。就是上传完成以后,返回的json..
this.responseWrite("{\"success\":true,\"msgs\":'" +uploadFileName+ "'}");
代码里面就这样写的, 可是通过调试,前台多了一个 <per> 标签。 json一直报错,郁闷的很,不知道为什么产生这个东西。
如果设置Response的contentType的话 FF就会出现文件下载的现象
为什么?
1
0
分享到:
评论
2 楼 atian25 2009-10-16  
^_^ +U
1 楼 peter2009 2009-10-15  
只能自立更生的解决问题了

在js脚本里加上

17 行
 enctype:'multipart/form-data', 

在JAVA脚本里

getResponse().setCharacterEncoding("UTF-8");
getResponse().setContentType("text/html");


就可以了

相关推荐

Global site tag (gtag.js) - Google Analytics