struts1上传注意事项 StrutsJavaApacheOSServlet
程序员文章站
2024-02-26 11:10:04
...
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处的路径必须加文件的名字
代码如下:
<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(); } } } }