JSP full File Upload

Source: Internet
Author: User
Tags file upload stmt tld

This article here for your friends to provide a free JSP complete file Upload Oh, if you are looking at this file upload code not to come in to see it, good crap not say much.

Actionform,
Package tester.business.maitain;

Import org.apache.struts.action.*;
Import javax.servlet.http.*;
Import org.apache.struts.upload.*;

public class Maintainform
Extends Actionform {

/** private String P_title;
private int post_index;
Private String Issue_time;
Private String issue;
Private String p_content;
Private String p_accessory;
private int p_id;
Private Java.util.ArrayList post_indexopts;
*/
Private Trainplan Trainplan = new Trainplan ();
Private Org.apache.struts.upload.FormFile TheFile1;
Private Org.apache.struts.upload.FormFile TheFile2;
Private Org.apache.struts.upload.FormFile TheFile3;

Public String Getp_title () {
return Trainplan.getp_title ();
}

public void Setp_title (String p_title) {
Trainplan.setp_title (P_title);
}

public void Settrainplan (Trainplan trainp) {
This.trainplan = TRAINP;

}

Public Trainplan Gettrainplan () {
return This.trainplan;

}

public void SetTheFile1 (Org.apache.struts.upload.FormFile theFile1) {
This.thefile1 = TheFile1;
}

Public Org.apache.struts.upload.FormFile GetTheFile2 () {
return theFile2;
}

public void SetTheFile2 (Org.apache.struts.upload.FormFile theFile2) {
This.thefile2 = TheFile2;
}

Public Org.apache.struts.upload.FormFile GetTheFile3 () {
return theFile3;
}

public void SetTheFile3 (Org.apache.struts.upload.FormFile theFile3) {
This.thefile3 = TheFile3;
}

Public Org.apache.struts.upload.FormFile GetTheFile1 () {
return theFile1;
}

Public actionerrors Validate (actionmapping actionmapping,
HttpServletRequest httpservletrequest) {
/** @todo: Finish This method, which is just the skeleton.*/
return null;
}

public void Reset (Actionmapping actionmapping,
HttpServletRequest httpservletrequest) {
}

}


JSP page corresponding to Actionform

Taglib uri= "/web-inf/struts-html.tld" prefix= "html"%>
<%@ taglib uri= "/web-inf/struts-bean.tld" prefix= "Bean"%>
<%@ taglib uri= "/web-inf/struts-logic.tld" prefix= "logic"%>
<%@ page contenttype= "text/html; Charset=utf-8 "%>
<%request.setcharacterencoding ("gb2312");%>
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<link href= ". /css/main.css "rel=" stylesheet "type=" Text/css ">
<title>add trainplan</title>

<body>
<table width= "border=" "0" align= "center" cellpadding= "0" cellspacing= "0" class= "Tabletoolbar" >
<tr>
&LT;TD class= "Tabletitletext" ><div align= "center" class= "Tabletitletext" >add trainplan</div></td >
</tr>
</table>
<!--dwlayouttable-->
<table width= "border=" "0" align= "center" cellpadding= "7" cellspacing= "0" class= "Inputframemain" >
<tr>
&LT;TD width= "3%" nowrap> </td>
&LT;TD width= "11%" nowrap><strong>title</strong></td>
&LT;TD width= "22%" Nowrap>&LT;TD width= "22%" Nowrap>&LT;TD width= "10%" nowrap><div align= "center" ></div></td>

</tr>
<tr>
<TD nowrap> </td>
<TD Nowrap><strong>post index</strong></td>
<TD nowrap>
</td>
<TD nowrap><div align= "center" ><strong>Issuer</strong></div></td>
<TD nowrap>


<TD height= "colspan=" 8 "nowrap><div align=" center "></div> <div align=" center ">
<tr>

</tr>

</div></td>
</tr>
</table>
<br>

<table width= "595" border= "0" align= "center" cellpadding= "7" cellspacing= "0" class= "Inputframemain" >
Train Plan Content
<tr>
<TD width= "10%" Nowrap><strong>trainplan content</strong>:<span class= "InputAreaCell" >< Strong><br>
</strong></span></td>
<TD width= "90%" >

</tr>


<tr>
<TD height= "colspan=" 8 "nowrap><div align=" center "></div> <div align=" center ">


</div></td>
</tr>
</table>
<br>
<table width= "610" border= "0" align= "center" cellpadding= "7" cellspacing= "0" class= "Inputframemain" >
Accessory
<tr>

<TD width= "595" ><span class= "Inputareacell" ><strong>accessory1
</div></td>
</tr>


</table>
</body>


Action page,
Package tester.business.maitain;

Import Tclcc.tester.util.Selector;

Import org.apache.struts.action.*;
Import javax.servlet.http.*;
Import java.util.*;
Import Org.apache.struts.upload.FormFile;
Import java.io.*;

Public class Maintainaction
Extends Action {
Public Actionforward Execute (actionmapping actionmapping,
Actionform Actionform,
HttpServletRequest HttpServletRequest,
HttpServletResponse httpservletresponse) {
/** @todo: Complete the business logic here, this is just a skeleton.*/
Maintainform maintainform = (maintainform) actionform;
Trainplandao Trainpdao = new Trainplandao ();
Trainplan Trainplan = new Trainplan ();
Trainplan = Maintainform.gettrainplan ();
Formfile thefile = null;
Thefile = Maintainform.getthefile1 ();
String p_accessory;
P_accessory = Thefile.getfilename ();
try {
InputStream stream = Thefile.getinputstream (); Read the file into
String FilePath = Httpservletrequest.getrealpath ("/"); Take the current system path
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
OutputStream BOS = new FileOutputStream (FilePath + "sub" + "/" +
Thefile.getfilename ()); Create an output stream for an uploaded file
System.out.println (filepath+ "/" +file.getfilename ());
int bytesread = 0;
byte[] buffer = new byte[8192];
while ((bytesread = stream.read (buffer, 0, 8192))!=-1) {
Bos.write (buffer, 0, bytesread); Write file to Server
}
Bos.close ();
Stream.Close ();
catch (Exception e) {
System.err.print (e);
}

try {
Trainplan.setp_accessory (p_accessory);
Trainpdao.addtrainplan (Trainplan)//Call Database Insert method
SYSTEM.OUT.PRINTLN ("Success");

catch (Exception ex) {
}
Httpservletrequest.setattribute ("Trainplain", Trainplan);
Return (Actionmapping.findforward ("trainplancreated"));
}
}


Package tester.business.maitain;

Import Tclcc.tester.business.maitain.Trainplan;
Import Tclcc.tester.util.DBConn;
Import java.sql.*;
Import java.util.*;

public class Trainplandao {
Private Dbconn dbconn = null;
PRIVATE Connection conn = null;

private static final String add_trainplan=
"INSERT into Ks_trainplan (p_id,p_title,issue_time,issuer,p_content,p_accessory) VALUES (Lpad Seq_p_id.nextval,10,´ 0´),?,?, to_date (?, ´yyyy-mm-dd´),?,?,?) ";
Private final static String update_trainplan=
"UPDATE Ks_trainplan set P_id=?,p_title=?,post_index=?,issue_time=to_date (?, ´yyyy-mm-dd´), Issuer=?,p_content=?,p_ Accessory=? where p_id=? ";

/**
* Get a connection a DB pool
* @return Connection
*/
Public Trainplandao () {
try {
Dbconn = new Dbconn ();
conn = Dbconn.getconnection ();
catch (Exception ex) {
Ex.printstacktrace ();
}
}

/**
* Add a row into DB
* @param examinee examinee
* @throws SQLException
*/
public void Addtrainplan (Trainplan Trainplan) throws SQLException {
PreparedStatement pstmt = null;

try {
pstmt = Conn.preparestatement (Add_trainplan);
Pstmt.setstring (1, Trainplan.getp_title ());
Pstmt.setstring (2, Trainplan.getissue_time ());
Pstmt.setstring (3, Trainplan.getissuer ());
Pstmt.setstring (4, Trainplan.getp_content ());
Pstmt.setstring (5, Trainplan.getp_accessory ());

Pstmt.executeupdate ();
catch (SQLException ex) {
Ex.printstacktrace ();
finally {
try {
Pstmt.close ();
Conn.close ();
catch (SQLException Ex1) {
}
}
}

/**
* Remove a row from DB
* @param Trainplan Trainplan
* @throws SQLException
*/
public void Removetrainplan (Trainplan Trainplan) throws SQLException {
This.removetrainplan (trainplan.getp_id ());
}

/**
* Remove a row from DB
* @param p_id int
* @throws SQLException
*/
public void Removetrainplan (int p_id) throws SQLException {
Statement stmt = null;
try {
stmt = Conn.createstatement ();
Stmt.execute ("DELETE from Ks_trainplan WHERE p_id=" + p_id);
catch (SQLException ex) {
Ex.printstacktrace ();
throw new SQLException ("Sqlexction On:TrainPlanDAO.removeExaminee ()");
finally {
try {
Conn.close ();
catch (SQLException Ex1) {
Ex1.printstacktrace ();
}
}
}

/**
* Update a row
* @param examinee examinee
* @throws SQLException
*/
public void Updatetrainplan (Trainplan Trainplan) throws SQLException {
PreparedStatement pstmt = null;
try {
Update_trainplan = Update_trainplan + "where p_id =" + trainplan.getp_id (); The condition need modification
pstmt = Conn.preparestatement (Update_trainplan);
Pstmt.setint (1, trainplan.getp_id ());
Pstmt.setstring (2, Trainplan.getp_title ());
Pstmt.setint (3, Trainplan.getpost_index ());
Pstmt.setstring (4, Trainplan.getissue_time ());
Pstmt.setstring (5, Trainplan.getissuer ());
Pstmt.setstring (6, trainplan.getp_content ());
Pstmt.setstring (7, Trainplan.getp_accessory ());
Pstmt.setint (8,trainplan.getp_id ());
Pstmt.executeupdate ();
}/*catch (SQLException ex) {
Ex.getstacktrace ();
throw new SQLException ("Sqlexction On:TrainPlanDAO.updateExaminee ()");
} */
catch (SQLException Sqle) {
Todo
{
System.err.println ("Exception occoured:message:" +sqle.getmessage ());
System.err.println ("SQL State:" +sqle.getsqlstate ());
SYSTEM.ERR.PRINTLN ("Vendor Code:" +sqle.geterrorcode () + "---------------");
while ((Sqle=sqle.getnextexception ())!=null);
}finally {
try {
Conn.close ();
catch (SQLException Ex1) {
Ex1.printstacktrace ();
}
}
}

/**
* Get all of record from DB
* @throws SQLException
* @return Collection
*/
Public Collection Getal L () throws SQLException {
Statement stmt = null;
ResultSet rs = null;
Trainplan Trainplan = null;
Collection list = null;

try {
stmt = Conn.createstatement ();
rs = Stmt.executequery ("SELECT * from Ks_trainplan");
List = new ArrayList ();
while (Rs.next ()) {
Trainplan = new Trainplan ();
TRAINPLAN.SETP_ID (Rs.getint ("p_id"));
Trainplan.setp_title (rs.getstring ("P_title"));
Trainplan.setpost_index (Rs.getint ("Post_index"));
Trainplan.setissue_time (rs.getstring ("Issue_time"));
Trainplan.setissuer (rs.getstring ("issuer"));
Trainplan.setp_content (rs.getstring ("p_content"));
Trainplan.setp_accessory (rs.getstring ("p_accessory"));
List.add (Trainplan);
}
catch (SQLException ex) {
finally {
try {
Conn.close ();
catch (SQLException Ex1) {
Ex1.printstacktrace ();
}
return list;
}
}

public static String toString (Trainplan Trainplan) {
return Trainplan.getp_title ();
}

/**
* For Test
* @param args string[]
*/


}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.