/**
*
* Operating system: WIN-XP
* Background program: Java
* Front-end script: javascript/extjs/html
*
* Tried: Submitted by the form submission, in the background request.getinputstream () to the value of * is null, and *request.getcontentlength () can be normal value. Using Request.getread () throws an error, the request has been *called after the network check, it seems that struts filtered out. Find another way, and then you have the following code.
*/
@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
//Check file
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;
}
}
To this Java code is complete:
Next is the script submit after found the upload progress bar has been there to walk around, I dropped a kidney, another Baidu. Because I do not jump, Baidu on many methods in my this seems unworkable. Had to use Ajax instead.
/*
* Ajax that part of the script is as follows
*/
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",/* found this form seems a little different ... ExtJS directly written form seems to have a problem? */
Success:function (response)
{
var str = response.responseText.substring (response.responseText.indexOf (' {'), RESPONSE.RESP Onsetext.indexof ('} ') +1);//The front-desk script returned to be <pre> frame up. Well, for normal use, find out the JSON manually.
var objformdata = Ext.util.JSON.decode (str);
if (objformdata.errno==0)
{
Ext.Msg.show ({
Title: ' Hints ',
Buttons:Ext.MessageBox.OK,
Msg: ' Upload success! ‘,
fn:function (button,e)
{
//upload successful close upload attachment window
var window = ext.getcmp (' Selectfileid ');
window.close ();
//Refresh List
var win= ext.getcmp (' Billfileid ');
var Grid = Win.findbyid (' Billfile_grid ');
var store = Grid.store;
Store.load ({
Params:params
});
}
});
}
}
});
/*
* The last code to post a form
*/
var fpanel=new ext.panel ({
Layout: ' Form ',
HEIGHT:40,
HTML: ' <form id= ' ufile ' enctype= ' multipart/form-data ' > Please select attachment <input type= ' file ' name= ' upload ' size= ' > </form> '
});
This article is from the "myplace" blog, make sure to keep this source http://bogy324.blog.51cto.com/4330880/1602335
Java JSP struts2.x File upload