`
Vince_Lee
  • 浏览: 61248 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

struts2上传

阅读更多

1.Action类

 

package com.bc.web;

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.text.SimpleDateFormat;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.bc.bean.InformationParentSheet;
import com.bc.bean.InformationSheet;
import com.bc.service.impl.userServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

public class AddInfoAction extends ActionSupport {
	private userServiceImpl userService;
	private InformationSheet is;
	private InformationParentSheet ips;
	private static final long  serialVersionUID =  572146812454l ;
    private static final int  BUFFER_SIZE =  16 * 1024 ;
   
    private  File myFile;
    private  String contentType;
    private  String fileName;
    private  String imageFileName;
   
    public void  setMyFileContentType(String contentType) {
        this .contentType =  contentType;
   } 
   
    public void  setMyFileFileName(String fileName) {
        this .fileName =  fileName;
   } 
       
    public void  setMyFile(File myFile) {
        this .myFile =  myFile;
   } 
   
    public  String getImageFileName() {
        return  imageFileName;
   } 
   
   
     public InformationParentSheet getIps() {
 		return ips;
 	}

 	public void setIps(InformationParentSheet ips) {
 		this.ips = ips;
 	}

 	public userServiceImpl getUserService() {
 		return userService;
 	}

 	public void setUserService(userServiceImpl userService) {
 		this.userService = userService;
 	}

 	public InformationSheet getIs() {
 		return is;
 	}

 	public void setIs(InformationSheet is) {
 		this.is = is;
 	}
   
    private static void  copy(File src, File dst) {
        try {
           InputStream in  = null ;
           OutputStream out  = null ;
            try {                
               in  = new  BufferedInputStream( new  FileInputStream(src), BUFFER_SIZE);
               out  = new  BufferedOutputStream( new  FileOutputStream(dst), BUFFER_SIZE);
                byte [] buffer = new byte [BUFFER_SIZE];
                while (in.read(buffer) > 0 ) {
                   out.write(buffer);
               } 
            } finally {
                if ( null !=  in) {
                   in.close();
               } 
                 if ( null !=  out) {
                   out.close();
               } 
           } 
        } catch (Exception e) {
           e.printStackTrace();
       } 
   } 
   
    private static  String getExtention(String fileName) {
//    	fileName=ServletActionContext.getRequest().getParameter("myFile");
        int  pos =  fileName.lastIndexOf(".");
        return  fileName.substring(pos);
   } 

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		String select=ServletActionContext.getRequest().getParameter("select");
		Long ifID=Long.parseLong(select);
		
		String ifHead=ServletActionContext.getRequest().getParameter("ifHeadLine");
		
		String ifrec=ServletActionContext.getRequest().getParameter("ifRec");
		
		//上传图片
		if(fileName!=null){
			imageFileName = new  Date().getTime() +  getExtention(fileName);
	        File imageFile = new  File(ServletActionContext.getServletContext().getRealPath("/UploadImages")+"/"+imageFileName);
//			File imageFile = new  File("F://uploadImg"+"/"+imageFileName);
	        copy(myFile, imageFile);
		}
		
		
		
		InformationParentSheet ips=userService.findInfoParSheetById(ifID);
		is.setInformationParentSheet(ips);
		
		is.setIfHeadline(ifHead);
		is.setIfRec(ifrec);
//		is.setIfPicture(imageFile.toString());
		is.setIfPicture(imageFileName);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 Date date = new Date();
		 String now = sdf.format(date);
		 date = sdf.parse(now);
		is.setIfPtTime(date);

		this.userService.addInfoSheet(is);
		System.out.println("添加完成!");
		return SUCCESS;
	}
}
 

2.jsp

 

<s:file name="myFile" label="Image File"></s:file>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ include file="_SessionCheck.jsp" %>


<html>
<head>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<SCRIPT type="text/javascript">
	function checkInput()
	{
		var ifTitle=document.getElementById("ifTitle").value;
		var list=document.getElementById("list").value;
		var ifCome=document.getElementById("ifCome").value;
		var ifSummary=document.getElementById("ifSummary").value;
		
		if(ifTitle==null||ifTitle=="")
		{
			alert('新闻标题不能为空!');
			return false;
		}
		if(list=="-1")
		{
			alert('请选择新闻类型!');
			return false;
		}
		if(ifCome==null||ifCome=="")
		{
			alert('新闻来源不能为空!');
			return false;
		}
		if(ifSummary=="")
		{
			alert('新闻概要不能为空!');
			return false;
		}
		
	}
	
</SCRIPT>
<title>新闻添加</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="keywords" content="Discuz!,Board,Comsenz,forums,bulletin board,">
<meta name="description" content="新闻  - Discuz! Board">
<meta name="generator" content="Discuz! 4.0.0RC4 with Templates 4.0.0">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
<meta http-equiv="MSThemeCompatible" content="Yes">

<style type="text/css"><!--
a			{ text-decoration: none; color: #000000 }
a:hover			{ text-decoration: underline }
body			{ scrollbar-base-color: #F3F6FA; scrollbar-arrow-color: #4D76B3; font-size: 12px; background-color: #ffffff }
table			{ font: 12px Verdana,Tahoma; color: #000000 }
input,select,textarea	{ font: 11px Verdana,Tahoma; color: #000000; font-weight: normal; background-color: #F3F6FA }
select			{ font: 11px Verdana,Tahoma; color: #000000; font-weight: normal; background-color: #F3F6FA }
.nav			{ font: 12px Verdana,Tahoma; color: #000000; font-weight: bold }
.nav a			{ color: #000000 }
.header			{ font: 11px Verdana,Tahoma; color: #FFFFFF; font-weight: bold; background-image: url("images/green/bg01.gif") }
.header a		{ color: #FFFFFF }
.category		{ font: 11px Verdana,Tahoma; color: #000000; background-color: #EFEFEF }
.tableborder		{ background: #4D76B3; border: 0px solid #4D76B3 } 
.singleborder		{ font-size: 0px; line-height: 0px; padding: 0px; background-color: #F3F6FA }
.smalltxt		{ font: 11px Verdana,Tahoma }
.outertxt		{ font: 12px Verdana,Tahoma; color: #000000 }
.outertxt a		{ color: #000000 }
.bold			{ font-weight: bold }
.altbg1			{ background: #F3F6FA }
.altbg2			{ background: #FFFFFF }
--></style>
<script language="JavaScript" src="images/common.js"></script>
<style type="text/css" id="defaultPopStyle">.cPopText { font-family: Tahoma, Verdana; background-color: #FFFFCC; border: 1px #000000 solid; font-size: 12px; padding-right: 4px; padding-left: 4px; height: 20px; padding-top: 2px; padding-bottom: 2px; visibility: hidden; filter: Alpha(Opacity=80)}</style></head><body leftmargin="0" rightmargin="0" topmargin="0" onkeydown="if(event.keyCode==27) return false;"><div id="popLayer" style="position: absolute; z-index: 1000;" class="cPopText"></div>

<br>
<s:form method="post" name="NewsAdd" action="addInfo" onsubmit="return checkInput()"  enctype ="multipart/form-data"> <!-- 调用action -->

<table class="tableborder" align="center" cellpadding="4" cellspacing="1" width="97%">
<tbody><tr>
<td colspan="2" class="header">新闻添加 - 内容</td>
</tr>
<tr>
<td class="altbg1" width="21%">新闻标题:</td>
<td class="altbg2"><s:textfield id="ifTitle" name="is.ifTitle" label="新闻标题"></s:textfield>
</tr>

<tr>
<td class="altbg1">是否头条:</td>
<td class="altbg2">
	<select name="ifHeadLine">
		<option value="否">否</option>
		<option value="是">是</option>
	</select>
</td>
</tr>

<tr>
<td class="altbg1">是否推荐:</td>
<td class="altbg2">
	<select name="ifRec">
		<option value="否">否</option>
		<option value="是">是</option>
	</select>
</td>
</tr>

<tr>
<td class="altbg1">新闻类型:</td>
<td class="altbg2">
	<s:select list="#request.list" id="list" listKey="ipId" 
    	   listValue="ipName" name="select" label="新闻类型:" headerValue="--请选择--" headerKey="-1">
	</s:select>
</td>
</tr>

<tr>
<td class="altbg1">作者:</td>
<td class="altbg2"><s:textfield name="is.ifAuthor" label="作者"></s:textfield></td>
</tr>

<tr>
<td class="altbg1">新闻来源:</td>
<td class="altbg2">
	<s:textfield id="ifCome" name="is.ifCome" label="出处"></s:textfield>
</td>
</tr>
<tr>
<td class="altbg1">备注:</td>
<td class="altbg2"><s:textfield name="is.ifRemark" label="备注"></s:textfield></td>
</tr>
<tr>

<td class="altbg1">新闻关键字:</td>
<td class="altbg2"><s:textfield name="is.ifKeyWord" label="新闻关键字"></s:textfield></td>
</tr>

<tr>
<td class="altbg1">新闻概要:</td>
<td class="altbg2"><s:textarea id="ifSummary" name="is.ifSummary" label="新闻概要" cols="150" rows="5"></s:textarea></td>
</tr>

<tr>
<td class="altbg1">新闻图片:</td>
<td class="altbg2">
	<s:file name="myFile" label="Image File"></s:file>
</td>
</tr>

<tr>
<td class="altbg1" valign="top">新闻内容:</td>
<td class="altbg2"><textarea id="ifContent" name="is.ifContent" id="is.ifContent" labelposition="left" cols="150" rows="15" id="descr"></textarea></td>

</tr>
</tbody></table>
<br>
<center><s:submit name="NewsSubmit"  value="提    交" /></center>
</s:form>
<script type="text/javascript"> 
       CKEDITOR.replace( 'is.ifContent' ); 
</script> 
</body></html>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics