1.
<form action="<%=basePath%>priRing.do?method=upload" method="post" enctype="multipart/form-data">
<table>
<tr>
<td><input type="file" name="formfile"/></td>//1
</tr>
<tr>
<td><input type="submit"/></td>
</tr>
</table>
form中
private FormFile formfile;//2
1和2两处的名字必须一样
2.
String name=ff.getFileName();
System.out.println("name="+name);
InputStream is=ff.getInputStream();
FileOutputStream fos=new FileOutputStream(path+"/"+ff.getFileName());//3
3处的路径必须加文件的名字
代码如下:
package com.buybal.mgr.struts.action.pictureAndRing;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import org.directwebremoting.util.Logger;
import com.buybal.mgr.struts.form.PictureAndRingForm;
import com.jrt.mgr.Global;
import com.jrt.mgr.bean.PictureAndRing;
public class PictureAndRingAction extends DispatchAction {
private static final Logger logger=Logger.getLogger(PictureAndRingAction.class);
public ActionForward pictureUpload(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
PictureAndRingForm fileForm=(PictureAndRingForm)form;
try{
//将文件进行上传
response.setCharacterEncoding("utf-8");
FormFile ff=fileForm.getFile();
if(ff.getFileSize()<0||ff.getFileName().equals("")){
logger.info("上传的文件不存在");
return null;
}
String path="D:\\upload";//路径可以通过配置文件获得
File file=new File(path);
if(!file.exists()){//如果文件不存在则建立该目录
file.mkdirs();
}
InputStream is=ff.getInputStream();
OutputStream bos = new FileOutputStream(path+"\\"+ff.getFileName());//此处的路径是:path+文件名
int bytesRead = 0;
byte buffer[] = new byte[ff.getFileSize()];
while ((bytesRead = is.read(buffer, 0, ff.getFileSize())) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.close();
is.close();
//以下方法是将Zip文件进行解压
int type=fileForm.getType();//上传内容,0图片,1是铃声
int contenttype=fileForm.getContenttype();//上传内容
unzip(path,type,contenttype);
System.out.println("dd");
}catch(Exception e){
e.printStackTrace();
logger.error("上传文件失败,失败原因是:"+e.toString());
}
return null;
}
/*解压文件
* **/
public synchronized void unzip(String path,int type,int contenttype) throws Exception{
File fff=new File(path);
File[] f=fff.listFiles();
for(int i=0;i<f.length;i++){
File fs=f[i];
System.out.println(fs.getName());
if(fs.getName().indexOf(".zip")>-1){
ZipFile zip=new ZipFile(fs);
path=path+"\\"+fs.getName().substring(0, fs.getName().indexOf(".zip"));
Enumeration e = zip.entries();
ZipInputStream iss=new ZipInputStream(new FileInputStream(fs));
while(e.hasMoreElements()){
ZipEntry zipEnt = (ZipEntry) e.nextElement();
if(zipEnt.isDirectory()){
continue;//如果是根目录则跳出
}
String name= (new String(zipEnt.getName().getBytes("ISO-8859-1"),"UTF-8")).trim();
File ff=new File(path);
if(!ff.exists()){
ff.mkdirs();
}
if(name.indexOf("/")>-1){//文件包含子目录时
name=name.substring(name.indexOf("/")+1,name.length());
}
if(name.indexOf("\\")>-1){//包含子目录时
name=name.substring(name.indexOf("\\")+1,name.length());
}
// InputStream iss = zip.getInputStream(zipEnt);
OutputStream os = new FileOutputStream(path+"\\"+name); //循环中不能用BufferedInputStream,会报stream Close错误
int bytesRead = 0;
byte buffer[] = new byte[8192];
while ((bytesRead = iss.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
//将上传的图片存入数据库
String fpath=path+"\\"+name;
List<PictureAndRing> list=Global.parDAO.getPictureAndRing(type, contenttype, fpath);
if(list==null||list.isEmpty())//检查数据库中是否有改图片
Global.parDAO.addPictureAndRing(name,type,contenttype,fpath);
}
iss.close();
}
}
}
}
分享到:
相关推荐
用struts2开发配置注意事项。
详细介绍了struts文件上传的原理机制和注意事项
注意事项: 1 TOMCAT安装路径中请不要包含中文字符,否则程序将不能正常运行. 目录及文件: debug\document PDM及数据库脚本。 作者留言: 本程序是作者学习struts spring hibernate构架后为了练习开发的一个小程序...
注意事项: 1 TOMCAT安装路径中请不要包含中文字符,否则程序将不能正常运行. 目录及文件: debug\document PDM及数据库脚本。 作者留言: 本程序是作者学习struts spring hibernate构架后为了练习开发的一个小程序...
注意事项: 1, 运行项目前请确认db.properties配置文件中的数据库连接参数是否正确 2, 如果打开properties文件出现乱码情况, 将此文件编码设为utf-8即可 3, 请保证数据库默认编码为utf8, 否则可能会出现中文乱码问题...
Jsp在线音乐管理系统源码 源码描述: 一、源码介绍 Jsp在线音乐管理系统源码通过AJAX+JSP+Struts 2.X+Mysql实现,mysql导入sql文件,修改DBConnection.java文件 ...三、注意事项 开发环境为jdk1.6,数据库为mysql
8. Struts2中的文件上传与下载:注意事项:1)导入jar包commons-io、commons-fileupload;2)表单提交方式必须是post;3)表单enctype属性必须为multipart/form-data。 二、Mybatis框架 1. MyBatis是一个基于Java...
本系统是基于struts+hibernate+java的论坛源代码,仅做初学者学习之余,界面仅做简单table处理,主要功能已实现,安装部署的同学,需要注意下打包中的 注意事项文件。 另外本人还上传资源 在线音乐网站、网络会议室...
源码描述: 一、源码介绍 Jsp在线音乐管理系统源码通过AJAX+JSP+Struts 2.X+Mysql实现,mysql导入sql文件,修改DBConnection.java文件 ...二、主要功能 ...三、注意事项 开发环境为jdk1.6,数据库为mysql
六、旅游服务在线留言功能:游客可以通过网站查看一些景区旅游的注意事项和在景区旅游经常遇到的一些问题,管理员在后台可以对信息进行更新和修改、删除功能。 该系统功能完善、界面美观、操作简单、功能齐全、...
在应用服务器中使用Hibernate的注意事项 12.3. JDO 12.3.1. 建立PersistenceManagerFactory 12.3.2. JdoTemplate和JdoDaoSupport 12.3.3. 基于原生的JDO API实现DAO 12.3.4. 事务管理 12.3.5. JdoDialect ...
在应用服务器中使用Hibernate的注意事项 12.3. JDO 12.3.1. 建立PersistenceManagerFactory 12.3.2. JdoTemplate和JdoDaoSupport 12.3.3. 基于原生的JDO API实现DAO 12.3.4. 事务管理 12.3.5. JdoDialect ...