SSH上传图片action获取为null 烦了几天了 大神们帮忙看看吧 我快不行了

这是jsp中的代码



文章标题



自定义属性
文章作者

发布时间

        <div class="add-remark">
            <span class="add-remark-sapn">内容摘要</span>
            <textarea class="add-remark-textarea" name="remark" placeholder="请输入内容摘要部分"></textarea>
        </div>

     <div class="add-img">
             <span class="add-img-span">封面图片</span> 
             <div class="add-img-div"><span>上传图片</span></div> 
           <input id="file" name="file" class="filepath"  onchange="changepic(this)" type="file" /> 

              <img src="images/0.jpg" id="show" class="srcimg"> 

        </div>


        <div class="add-content">
            <span class="add-content-span">文章内容</span>
                        <textarea class="add-content-rextarea" name="contents">

            </textarea>
        </div>

        <div class="add-opration">
            <input type="submit" class="add-opration-submit" value="立即提交"/>
            <input type="reset" class="add-opration-reset" value="重置" />

这是action中的代码

package com.aaa.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.swing.JOptionPane;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.aaa.entity.AAA_news;
import com.aaa.service.AAAservice;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class NewsAction extends ActionSupport implements ModelDriven{
private static final long serialVersionUID = 1L;
public AAAservice service;
private AAA_news news=new AAA_news();
private File file;
private String fileFileName;
private String fileContentType;

public File getFile() {
return file;
}

public void setFile(File file) {
    this.file = file;
}


public String getFileFileName() {
    return fileFileName;
}


public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
}


public String getFileContentType() {
    return fileContentType;
}


public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
}

public AAA_news getModel() {

    return news;
}


public AAAservice getService() {
    return service;
}

public void setService(AAAservice service) {
    this.service = service;
}

public AAA_news getNews() {
    return news;
}

public void setNews(AAA_news news) {
    this.news = news;
}


public String saveNews() throws Exception { 

    System.out.println(news.getTitle()+news.getWriter()+news.getDate()
            +news.getRemark()+news.getFile()+news.getContents());
    String image="images";
      JOptionPane.showMessageDialog(null,"44444..."+this.file);
    if(file!=null){
        String filePath=ServletActionContext.getServletContext().getRealPath(image);
        String str=this.fileFileName;
        String s=str.substring(str.length()-4,str.length());
        String filename=image+"/"+news.getDate()+s;     
        try {
            File flie=new File(filePath);
            //获取运行目录
            if(!flie.exists()){//检查文件是否存在,不存在自动创建
                flie.mkdir();
            }
            FileUtils.copyFile(file, new File(filePath,filename));

            news.setFile("images/"+filename);
            this.service.saveNews(news);
        } catch (Exception e) {
            e.printStackTrace();
            return NewsAction.INPUT;
        }
    }
    return NewsAction.SUCCESS;
}

}

struts.xml中的配置

    <action name="savenews" class="news" method="saveNews">
     <interceptor-ref name="defaultStack"/>
        <interceptor-ref name="fileUpload">
            <param name="allowedTypes">application/pdf,text/plain,image/bmp,image/png</param>
            <param name="maximumSize">5242880</param>
        </interceptor-ref>

        <result name="success">/main.jsp</result>
         <result name="input">/addnews.jsp</result>

    </action>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值