struts之上传文件
用的是struts自带的org.apache.struts.upload.FormFile,这篇文章参考了网上一些资料,在这做个小结,为日后参考。
首先是提交页面,代码如下:
<form action="DownAddSubmit.do" method="post" ENCTYPE="multipart/form-data">
标题:<input size=80 name="downtitle" value=""/><br/>
图片:<input type="file" size="50" name="thePic"/><br/>
文件:<input type="file" size="50" name="theFile"/><br/>
<input type="submit" value="确定"/>
</form>
我这里假设上传2个文件,1个图片,1个别的文件,其中ENCTYPE="multipart/form-data"务必需要这样写的,当然这里也可以
首先是提交页面,代码如下:
<form action="DownAddSubmit.do" method="post" ENCTYPE="multipart/form-data">
标题:<input size=80 name="downtitle" value=""/><br/>
图片:<input type="file" size="50" name="thePic"/><br/>
文件:<input type="file" size="50" name="theFile"/><br/>
<input type="submit" value="确定"/>
</form>
我这里假设上传2个文件,1个图片,1个别的文件,其中ENCTYPE="multipart/form-data"务必需要这样写的,当然这里也可以
传其他的东西,我传了个标题,文本的,把这个标题传入数据库,把图片和文件上传到服务器空间,同时把这2个文件名字也传
到数据库。
先配置下struts-config.xml,如下:
<form-beans><form-bean name="downForm" type="Wap.DownForm" /></formbeans>
<action path="/DownAddSubmit" type="Wap.DownAddSubmitAction" name="downForm" scope="request"
<form-beans><form-bean name="downForm" type="Wap.DownForm" /></formbeans>
<action path="/DownAddSubmit" type="Wap.DownAddSubmitAction" name="downForm" scope="request"
input="/wrong.jsp"/>
接下来先编写DownForm.java,主要进行set和get。
package Wap;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*; //包的导入
public class DownForm extends ActionForm{
protected String downtitle;
protected FormFile theFile;
protected FormFile thePic;
public String getDowntitle(){
return downtitle;
}
public void setDowntitle(String downtitle){
this.downtitle=downtitle;
}
public FormFile getTheFile(){
return theFile;
}
public void setTheFile(FormFile theFile){
this.theFile=theFile;
}
public FormFile getThePic(){
return thePic;
}
public void setThePic(FormFile thePic){
this.thePic=thePic;
}
}这里可以增加验证,为了简便,省略。。。
接下来先编写DownForm.java,主要进行set和get。
package Wap;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.upload.*; //包的导入
public class DownForm extends ActionForm{
protected String downtitle;
protected FormFile theFile;
protected FormFile thePic;
public String getDowntitle(){
return downtitle;
}
public void setDowntitle(String downtitle){
this.downtitle=downtitle;
}
public FormFile getTheFile(){
return theFile;
}
public void setTheFile(FormFile theFile){
this.theFile=theFile;
}
public FormFile getThePic(){
return thePic;
}
public void setThePic(FormFile thePic){
this.thePic=thePic;
}
}这里可以增加验证,为了简便,省略。。。
DownForm.java编译后,编写DownAddSubmitAction.java
代码如下:
package Wap; //最重要的action
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
代码如下:
package Wap; //最重要的action
import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.struts.upload.*;
import javax.servlet.ServletContext;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import java.io.*;
import javax.servlet.http.*;
public final class DownAddSubmitAction extends Action{
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
DownForm downform=(DownForm)form;
FormFile theFile=downform.getTheFile();
FormFile thePic=downform.getThePic();
String downtitle=new String(downform.getDowntitle().trim().getBytes("iso-8859-1"),"utf-8"); //编码的转换
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
DownForm downform=(DownForm)form;
FormFile theFile=downform.getTheFile();
FormFile thePic=downform.getThePic();
String downtitle=new String(downform.getDowntitle().trim().getBytes("iso-8859-1"),"utf-8"); //编码的转换
HttpSession session = request.getSession();
Vector downList = new Vector();
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource)context.getAttribute("sqlserver");
DB db=new DB(dataSource); //这里需要DB数据库连接等操作
String PageForward;
Down down=new Down(); //这里需要Down这个bean,重要是为了想数据库传数据
down.setDowntitle(downtitle); //Down大太,这里不传了
//下面进行上传文件
try{
InputStream stream=theFile.getInputStream();
String filePath=context.getRealPath("/");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
OutputStream bos=new FileOutputStream(filePath+"/"+theFile.getFileName());
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=stream.read(buffer,0,8192))!=-1){
bos.write(buffer,0,bytesRead);
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
//下面进行上传图片
try{
InputStream istream=thePic.getInputStream();
String picPath=context.getRealPath("/");
ByteArrayOutputStream baos2=new ByteArrayOutputStream();
OutputStream bos2=new FileOutputStream(picPath+"/"+thePic.getFileName());
int bytesRead2=0;
byte[] buffer2=new byte[8192];
while((bytesRead2=istream.read(buffer2,0,8192))!=-1){
bos2.write(buffer2,0,bytesRead2);
}
bos2.close();
istream.close();
}catch(Exception e){
System.err.print(e);
}
String downpath=null;
String picpath=null;
try{
downpath=theFile.getFileName();
picpath=thePic.getFileName();
}catch(Exception e){
}
down.setDownpath(downpath);
down.setPicpath(picpath);
ActionMessages errors=new ActionMessages();
if(down.Add(db)){
PageForward="toAddsuccess";
}else{//省略错误机制
PageForward="toWrong";
}
db.close();
return (mapping.findForward(PageForward));
}
}
Vector downList = new Vector();
ServletContext context = servlet.getServletContext();
DataSource dataSource = (DataSource)context.getAttribute("sqlserver");
DB db=new DB(dataSource); //这里需要DB数据库连接等操作
String PageForward;
Down down=new Down(); //这里需要Down这个bean,重要是为了想数据库传数据
down.setDowntitle(downtitle); //Down大太,这里不传了
//下面进行上传文件
try{
InputStream stream=theFile.getInputStream();
String filePath=context.getRealPath("/");
ByteArrayOutputStream baos=new ByteArrayOutputStream();
OutputStream bos=new FileOutputStream(filePath+"/"+theFile.getFileName());
int bytesRead=0;
byte[] buffer=new byte[8192];
while((bytesRead=stream.read(buffer,0,8192))!=-1){
bos.write(buffer,0,bytesRead);
}
bos.close();
stream.close();
}catch(Exception e){
System.err.print(e);
}
//下面进行上传图片
try{
InputStream istream=thePic.getInputStream();
String picPath=context.getRealPath("/");
ByteArrayOutputStream baos2=new ByteArrayOutputStream();
OutputStream bos2=new FileOutputStream(picPath+"/"+thePic.getFileName());
int bytesRead2=0;
byte[] buffer2=new byte[8192];
while((bytesRead2=istream.read(buffer2,0,8192))!=-1){
bos2.write(buffer2,0,bytesRead2);
}
bos2.close();
istream.close();
}catch(Exception e){
System.err.print(e);
}
String downpath=null;
String picpath=null;
try{
downpath=theFile.getFileName();
picpath=thePic.getFileName();
}catch(Exception e){
}
down.setDownpath(downpath);
down.setPicpath(picpath);
ActionMessages errors=new ActionMessages();
if(down.Add(db)){
PageForward="toAddsuccess";
}else{//省略错误机制
PageForward="toWrong";
}
db.close();
return (mapping.findForward(PageForward));
}
}
然后随便写个正确页面就可以了。
还需要在struts-config.xml增加转向,简单,省略
还需要在struts-config.xml增加转向,简单,省略
在windows2003+tomcat4+struts1.1+mssql成功运行。
不足之处,还望高人指点,不胜感激!!!