当前位置: 代码迷 >> J2EE >> SSH上传图片 报错 新手 刚开始学SSH
  详细解决方案

SSH上传图片 报错 新手 刚开始学SSH

热度:747   发布时间:2016-04-17 23:15:39.0
SSH上传图片 出错 新手 刚开始学SSH
MerchandiseAction.java

public class MerchandiseAction extends BaseAction<Merchandise>{
        private File picture;

public File getFile() {
return picture;
}

public void setFile(File picture) {
this.picture = picture;
}
        public String add() throws Exception {
File picture=this.getFile();
String attr = model.getPicture();
// System.out.print(attr);
    String pname=FileHelp.upload(picture,attr);
model.setPicture(pname);
// System.out.print(pname);
merchandiseService.save(model);
return "toList";
}

File.java

package com.util;

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 javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

public class FileHelp {


//获得存放图片项目路径
public static String getPath(){
     HttpServletRequest request = ServletActionContext.getRequest();
     String realPath = request.getSession().getServletContext().getRealPath("");
return realPath;
    }
/* * 获取一个记录编号 * 格式:12 位时间+业务编号+随即数 * 
 * 例如:100330101028+04+893 (2010-03-30-10:20:28, 业务号是04, 随机数893 ) 
 * *@param btype 业务类型编号 员工业务01 招聘业务02…… 
 * *@return code 构造的一个记录编号 
*/ 
public static String getNo(){
//返回的code String code; 
//系统当前时间 12位  
SimpleDateFormat sdf=new SimpleDateFormat("yyMMddhhmmss");  
String nowDate=sdf.format(new java.util.Date()); 
//随机数  
String iRandom = Math.round(Math.random()*900 )+100 +""; 
//整合一个code 
return nowDate + iRandom ; 



//上传
public static String upload(File file,String name) throws Exception{
// 获得上传图片的后缀名
String aa = name.substring(name.lastIndexOf("."),name.length()-2);
// 在给定的路径下存放图片信息
String no = FileHelp.getNo();
File parentDir = new File(FileHelp.getPath()+"\\upload", no + aa);
String pname = "upload\\"+ no + aa;
// 读下给定路径的文件
File src = new File(FileHelp.getPath());
// 判断文件是否存在,不存在进行创建
if (!src.exists()) {
src.mkdirs();
}
// 创建输入流
InputStream in = null;
// 创建输出流
OutputStream out = null;
try {
// 上传文件写入请求流中
in = new FileInputStream(file);
// 读取流中的数据
byte[] b = new byte[in.available()];
in.read(b);
// 把本地读到的文件放到输出流里
out = new FileOutputStream(parentDir);
// 写出输出流
out.write(b);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
// 及时关闭输入和输出流
if (in != null)
in.close();
if (out != null)
out.close();
}
return pname;
}
}

添加.html

 <s:form action="merchandise_add" method="post" enctype="multipart/form-data">
 <s:hidden name="id"></s:hidden>
 
<article class="module width_full">
<header><h3>更改或设置新的商品</h3></header>
<div class="module_content">

<fieldset  style="width:45%; float:left;">
<label>名称</label>
<s:textfield name="merName"></s:textfield>
</fieldset>

<fieldset  style="width:45%; float:left;margin-left: 3%;">
<label>图片</label>
<s:file name="picture" label="照片" id="picture" contenteditable="false"/>
    
</fieldset>
<div class="clear"></div>
</div>
<footer>
<div class="submit_link">
  <input type="submit" value="发布" class="alt_btn">
</div>
</footer>
</article><!-- end of post new article --><!-- end of styles article -->
</s:form>

Struts.xml

<!-- 商品管理 -->
<action name="merchandise_*" class="merchandiseAction" method="{1}">
 

<result name="list">/WEB-INF/jsp/merchandiseAction/list.jsp</result>
<result name="saveUI">/WEB-INF/jsp/merchandiseAction/addUI.jsp</result>
<result name="toList" type="redirectAction">merchandise_list</result>
<!-- 限制图片的格式和图片的大小 -->
            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">
                    image/bmp,image/png,image/gif,image/jpeg,image/jpg
                </param>
                <param name="maximumSize">102400</param>
            </interceptor-ref>
            
            <!-- 默认的拦截器,必须要写 -->
            <interceptor-ref name="defaultStack" />
</action>


报错500


type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
java.io.FileInputStream.<init>(FileInputStream.java:138)
com.util.FileHelp.upload(FileHelp.java:60)
com.view.action.MerchandiseAction.add(MerchandiseAction.java:70)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:306)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
------解决思路----------------------
找到了
 private File picture;
 
    public File getFile() {
        return picture;
    }
 
    public void setFile(File picture) {
        this.picture = picture;
    }
你的 get set 都错了 
你的文件名叫picture   但是 你的 get set 看看都叫什么名 getFile  setFile  你这是因为你的代码是复制过来的吧 就改了一个picture   
你把 getFile  setFile   删除 重新对 picture进行get set
  相关解决方案