⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 addarticleaction.java

📁 交易撮合系统是一套买卖信息沟通的平台
💻 JAVA
字号:
package struts.houtaiaction;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.upload.FormFile;
import org.apache.struts.validator.DynaValidatorForm;

import dao.ArticleDao;
import dao.ArticleTypeDao;
import domain.ArticleKind;
import domain.Articles;

public class AddArticleAction extends Action{
    public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception{
    	
    	DynaValidatorForm dvf = (DynaValidatorForm)form;
    	String name = request.getParameter("name");
    	String title = request.getParameter("kind");
    	String add = request.getParameter("address");
    	String content = request.getParameter("content");
    	
    	int address = 0;
    	Articles a = new Articles();
    	    	        
        
      //开始上传文件
		FormFile fileone = (FormFile)dvf.get("imageupload") ; //取得上传的文件名
		System.out.println(fileone.getContentType());
		if(fileone.getFileSize() > 0){
			String s1 = fileone.getContentType();
			String kuozhan = s1.substring(6);
			String[] sty = {"jpg","gif","bmp","jpeg","JPG","GIF","BMP","JPEG","pjpeg"};
			int count = 0;
			for(int i = 0; i < sty.length ; i ++){
				if(sty[i].equals(kuozhan)){
					count = count + 1;
				}
			}
			if(count == 0 || count > 1){
				ActionMessages errros = new ActionMessages();
				errros.add("errors.image", new ActionMessage("errors.image"));
				this.saveErrors(request, errros);
				return mapping.findForward("no");
			}
			if(fileone.getFileSize() > 3500000){
				ActionMessages errros = new ActionMessages();
				errros.add("errors.imageSize", new ActionMessage("errors.imageSize"));
				this.saveErrors(request, errros);
				return mapping.findForward("no");
			}
			String filePath = this.getServlet().getServletContext().getRealPath("/") ; //取得当前路径
	        InputStream stream = fileone.getInputStream() ; //把文件读入
	        String path=filePath+"images\\articlephotos\\";
	        /*
	         * 建立一个上传文件的输出流如果是linux系统请把"\\" 换成 "/"
	         * 
	         * 现在文件上传的路径为-------->>>>>>  path
	         */
	        String iamgepath="images\\articlephotos\\"+fileone.getFileName();
	        a.setPhoto(iamgepath);
	        OutputStream bos = new FileOutputStream(path  +fileone.getFileName());
	        
	        request.setAttribute("fileName",filePath + "/" + fileone.getFileName());
	        int bytesRead = 0 ;
	        byte[] buffer = new byte[3500000] ;
	        while( (bytesRead = stream.read(buffer,0,3500000) ) != -1  )
	        {
	            bos.write(buffer,0,bytesRead) ;
	        }
	        bos.close();
	        stream.close();    
	        //上传文件完成
		}else{
			a.setPhoto("没有图片");
		}
		
		ArticleTypeDao atd = new ArticleTypeDao();
    	ArticleKind ak = atd.getArticleTypeID(title);
    	
        int kindid = ak.getKindid();
        if(add != null && add != ""){
        	address = Integer.parseInt(add);
        }
        ArticleDao ad = new ArticleDao();
        
        int c = ad.addArticle(name, kindid, address, content,a.getPhoto());
        if(c != 1){
        	return mapping.findForward("fail");
        }
        
    	return mapping.findForward("article");
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -