java jsp Struts2.X 文件上传

/**

*

*操作系统:WIN-XP

*后台程序:Java

*前端脚本:JavaScript/ExtJs/Html

*

*做过的尝试:用form的submit方式提交,在后台request.getInputStream()取到的值*为null,而*request.getContentLength()是可以正常取值的。用request.getRead()抛出错误,request has been *called 网查之后得知,似乎是Struts过滤掉了。另寻他法于是便有了下面代码。

*/

@ParentPackage(StrutsNamespace.PP_JSON)

@Namespace("")

@Action("savebillfile")

public class SaveBillFileAction extends SDBaseAction {

private static final long serialVersionUID = 5156288255337069381L;

private  String msg;

private int ErrNo;

private String contentType;

private File docmentFile;

private String fileName;

private Boolean success = false;

private String billtype;

private String billid;

public String upload() throws Exception {

Connection con =null;

Statement stm =null;

PreparedStatement pst=null;

ErrNo=0;

msg="";

if(!checkFile())

{

return SUCCESS;

}

if(docmentFile.isFile())

{

FileInputStream in = new FileInputStream(docmentFile);

try{

con=ConnectPools.getConnection(getSession());

Date dt=new Date();

String date = DateFormat.getDateInstance().format(dt);

String sSql = "insert into billfiles(billtype,billid,filename,fileext,uploaddate,filesize,opid,filedata)values(?,?,?,?,?,?,?,?)";

pst = con.prepareStatement(sSql);

pst.setString(1, billtype);

pst.setString(2, billid);

pst.setString(3, fileName);

pst.setString(4, fileName.substring(fileName.indexOf("."), fileName.length()));

pst.setString(5, date);

pst.setLong(6, in.available());

pst.setInt(7, ((EmployInfo)this.getSession().getAttribute("employ")).getId());

pst.setBinaryStream(8,in,in.available());

pst.executeUpdate();

}

catch(Exception e){

ErrNo=1;

msg=e.getMessage();

}

finally{

try{

pst.close();

stm.close();

con.close();

}catch(Exception e)

{

ErrNo=1;

msg=e.getMessage();

}

}

}

setSuccess(true);

return SUCCESS;

}

private boolean checkFile() {

// TODO Auto-generated method stub

//检查文件

return true;

}

public void setFileName(String fileName) {

this.fileName = fileName;

}

public void setUploadFileName(String fileName) {

this.fileName = fileName;

}

public void setUploadContentType(String contentType) {

this.contentType = contentType;

}

public void setUpload(File docmentFile) {

this.docmentFile = docmentFile;

}

public String getMsg() {

return msg;

}

public void setSuccess(Boolean success) {

this.success = success;

}

public Boolean getSuccess() {

return success;

}

public void setBilltype(String billtype) {

this.billtype = billtype;

}

public void setBillid(String billid) {

this.billid = billid;

}

public int getErrNo() {

return ErrNo;

}

public void setMsg(String msg) {

this.msg = msg;

}

public void setContentType(String contentType) {

this.contentType = contentType;

}

}

//到此java代码全了。。

//接下来是脚本submit之后发现上传的进度条一直在那里走来走去,我滴个肾,又是一番百度。由于我这不要跳转,百度上许多方法在我这似乎行不通。。只好改用了Ajax。。

/*

*  ajax那部分脚本如下

*/

var params={};

Ext.apply(params,{billtype:this.billType});

Ext.apply(params,{billid:this.billId});

Ext.Ajax.request({

url : ‘savebillfile!upload.action‘,

timeout : 10000,

scope : this,

params:params,

isUpload:true,

form:"ufile",/*发现这个form好像有点不一样。。。ExtJs直接写出来的form好像有问题?*/

success : function(response)

{

var str =                                 response.responseText.substring(response.responseText.indexOf(‘{‘),response.responseText.indexOf(‘}‘)+1);//前台脚本返回来的竟然被<pre>框起来了。好吧,为了正常使用,就手动把json找出来。

var objFormData = Ext.util.JSON.decode(str);

if(objFormData.ErrNo==0)

{

Ext.Msg.show({

title:‘提示‘,

buttons:Ext.MessageBox.OK,

msg: ‘上传成功!‘,

fn:function(button,e)

{

//上传成功关闭上传附件窗口

var window = Ext.getCmp(‘selectfileid‘);

window.close();

//刷新列表

var win= Ext.getCmp(‘billfileid‘);

var grid = win.findById(‘billfile_grid‘);

var store = grid.store;

store.load({

params:params

});

}

});

}

}

});

/*

* 最后贴出form的代码

*/

var fPanel=new Ext.Panel({

layout : ‘form‘,

height:40,

html:‘<form id="ufile" enctype="multipart/form-data">请选择附件<input type="file" name="upload" size="50"></form>‘

});

时间: 2024-08-24 03:07:10

java jsp Struts2.X 文件上传的相关文章

Java jsp servlet 实现文件上传 最简单操作

|--实现文件上传的方式 1.进入http://commons.apache.org/proper/commons-fileupload/using.html 2.点击用户指南 3.对着用户指南一步步复制粘贴,并把代码撸成自己想要的样子 补充:记得要将commons-io-2.4.jar和commons-fileupload-1.2.2.jar这两个包放到编译器里面 |--现成的代码 1 <%-- 2 Created by IntelliJ IDEA. 3 User: Administrator

struts2实现文件上传进度条(前端JS+Java)(自我整理)

需要做一个文件上传进度的效果,结合网上资料和自己的实践后,这里做一个整理 步骤如下: 1.重写.自定义JakartaMultiPartRequest类 <span style="font-size:12px;">package com.hikvision.fileUploadProcess.interceptor; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import

Struts2单文件上传原理及示例

一.文件上传的原理 表单元素的enctype属性指定的是表单数据的编码方式,该属性有3个值: 1.application/x-www-form-urlencoded:这是默认编码方式,它只处理表单域里的value属性值,采用这种编码方式的表单会将表单域的值处理成URL编码方式. 2.multipart/form-data:这种编码方式的表单会以二进制流的方式来处理表单数据,这种编码方式会把文件域指定文件的内容也封装到请求参数里. 3.text/plain:这种方式主要适用于直接通过表单发送邮件的

Struts2 文件上传,下载,删除

本文介绍了: 1.基于表单的文件上传 2.Struts 2 的文件下载 3.Struts2.文件上传 4.使用FileInputStream FileOutputStream文件流来上传 5.使用FileUtil上传 6.使用IOUtil上传 7.使用IOUtil上传 8.使用数组上传多个文件 9.使用List上传多个文件 ----1.基于表单的文件上传----- fileupload.jsp <body> <form action="showFile.jsp" na

关于Struts2单文件上传

要实现Struts2框架的文件上传,需要用到2个jar包 commons-fileupload-1.2.2.jar commons-io-2.0.1.jar 由于文件解析Struts2内部已经帮我们做好了,大大降低了开发难度,我们只需要在Action里设置好对应的参数,目录进行使用即可. 来个小示例: upload.jsp 这个页面的表单有三点需要注意的: 1.表单必须使用post方式提交 2.表单编码类型 enctype="multipart/form-data"   3.<s

Struts2实现文件上传报错(一)

1.具体报错如下 2014-5-1 23:02:38 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for servlet [jsp] in context with path [/UploadFile] threw exception [An exception occurred processing JSP page /file.jsp at line 28 25: <table> 26

Struts2实现文件上传(一)

Struts2实现文件上传 文件上传页面 file.jsp: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); Str

Struts2实现文件上传(二)

Struts2实现文件上传 文件上传页面 file.jsp: <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <% String path = request.getContextPath(); Str

Struts2完成文件上传

文件上传是WEB应用经常需要面对的问题.在大部分时候,用户的请求参数是在表单域输入的字符串,但如果为表单元素设置enctype="multipart/form-data"属性,则提交表单时候不再是以字符串方式提交请求参数,而是以二进制编码的方式提交请求,此时直接通过HttpServletRequest的getParameter方法就无法正常获取请求参数的值,而通过二进制流来获取请求内容,就可以获取到上传文件的内容,从而实现文件上传的功能. Struts2的文件上传支持在原有的文件上传项