不积跬步,无以至千里

博客园 首页 新随笔 联系 订阅 管理

前端页面的form需要如下设置
<form name="newsadd_form" action="NewsInsert.jsp" method="post" enctype="multipart/form-data" onSubmit="return checkForm();">
      </form>
后台代码如下:需要下载commons-fileupload.jar这个包,放在WEB-INF/lib目录下
<%@ page contentType="text/html; charset=GBK" language="java"%>
<%@page import="org.apache.commons.fileupload.*,java.io.File,java.io.IOException"%>
<%@page import="java.util.List"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.text.DecimalFormat"%>
<%@page import="com.linuxok.com.general.UUIDHexGenerator"%>
<%@page import="com.filemanage.interfaces.INewsFilesDAO"%>
<%@page import="com.filemanage.factory.NewsFilesFactory"%>
<%@page import="com.linuxok.com.general.FormatDate"%>
<%@page import="com.filemanage.dto.News"%>
<%@page import="com.filemanage.factory.NewsFactory"%>
<%@page import="com.filemanage.dto.Channel"%>
<%@page import="com.filemanage.factory.ChannelFactory"%>
<% request.setCharacterEncoding("GBK"); 
String channelId = "";
String title = "";
String news_type = "";
String pic_file = "";
String author = "";
String content_summ = "";
String content = "";
String ispinglun = "";
String option = "";

String picPath = getServletContext().getRealPath("/")+"UserFiles/Image/";
String filePath = getServletContext().getRealPath("/")+"UserFiles/File/";
String tmpDirectory = getServletContext().getRealPath("/")+"tmp/";  // 申明临时目录
int maxPostSize = 4 * 1024 * 1024;    // 申明限制上传文件总大小为, 单位为 byte, -1 表示无限制  
DecimalFormat df = new DecimalFormat("00");
int m = 0;
String id = UUIDHexGenerator.getUUIDHex();
INewsFilesDAO newsFile = NewsFilesFactory.newInstance();

   try{
        DiskFileUpload fu = new DiskFileUpload();
        fu.setSizeMax(maxPostSize);                 //设置文件大小.

        fu.setSizeThreshold(4096);              //设置缓冲大小.

        fu.setRepositoryPath(tmpDirectory);      //设置临时目录.
  
  List fileItems = fu.parseRequest(request);   //解析请求,返回一个集合.
        
        Iterator i = fileItems.iterator();

        while(i.hasNext()) {
     
            FileItem fi = (FileItem)i.next();
            
   //这是用来确定是否为文件属性, 
   if(fi.isFormField()) {

 //  String fieldName = fi.getFieldName();     //这里取得表单名
   if (fi.getFieldName().equals("channelId")) channelId = fi.getString("gbk");
   if (fi.getFieldName().equals("title")) title = fi.getString("gbk");
   if (fi.getFieldName().equals("news_type")) news_type = fi.getString("gbk");
   if (fi.getFieldName().equals("author")) author = fi.getString("gbk");
   if (fi.getFieldName().equals("content_summ")) content_summ = fi.getString("gbk");
   if (fi.getFieldName().equals("content")) content = fi.getString("gbk");
   if (fi.getFieldName().equals("ispinglun")) ispinglun = fi.getString("gbk");
    } else { //这里开始外理文件
                 String fileName = fi.getName();   // 返回文件名包括客户机路径
                 long size = fi.getSize();
                 if(fileName!=null && !fileName.equals("")) {
  fileName = fileName.replace('\\','/');
  String ext = fileName.substring(fileName.lastIndexOf("."),fileName.length());
      
  if (fi.getFieldName().equals("pic_file")){
       pic_file = FormatDate.getDate("yyyyMMddHHmmss") + df.format(m)+ ext;
   fi.write(new File(picPath+pic_file));     // 写文件到服务器.
  }
  //附件处理
  if(fi.getFieldName().equals("option")){
  option = FormatDate.getDate("yyyyMMddHHmmss") + df.format(m++) + ext;
  if(!ext.equals(".exe")){
    if(newsFile.insert(id,fileName,option) ){
    fi.write(new File(filePath+option));     // 写文件到服务器.
    }
  }
  }
  
       }
    }
         }
   }
  catch(Exception e){
  %>  


<script language="JavaScript">
alert("文件上传错误.");
history.back();
</script>
<% return; 
}
  Channel channel = ChannelFactory.newInstance().getRecord(channelId);
News news = new News();
news.setId(id);
news.setChannel_id(channelId);
news.setTitle(title);
news.setNews_type(news_type);
news.setAuthor(author);
news.setIssue_state(channel.getFlag()); // 0:未审核, 1:已审核
news.setContent_summ(content_summ);
news.setContent(content);
news.setIspinglun(ispinglun);
news.setPic_file(pic_file);
if(NewsFactory.newInstance().insert(news)){
response.sendRedirect("NewsList.jsp?channelId="+channelId);
} else{
%>
<script language="JavaScript">
alert("信息提交失败,请重新提交");
history.back();
</script>
<%}%>

posted on 2016-04-20 10:31  Zeroassetsor  阅读(386)  评论(0)    收藏  举报