上传文件的配置
<!-- ajax文件上传需要 --> <script type="text/javascript" src="../js/jquery.form.js"></script> <script type="text/javascript"> $("#picForm").ajaxForm({ url:"${ctx}/admin/goods/imgUpload", type:"POST", dataType:"text", async:true, success:function(){ alert("上传成功"+imgUrl); tinyMCE.execCommand("mceInsertContent",false,"<img src=${ctx}/images/goodsDescImgs/"+imgUrl+">"); }, error:function(){ alert("文件上传失败!"); } }); </script>
<tr> <td class="tableleft"></td> <td> <form id="picForm" method="POST" enctype="multipart/form-data"> <input type="file" name="pic"/> </form> <button type="submit" class="btn btn-primary" form="picForm">上传</button> <button type="submit" class="btn btn-primary">保存</button> <button type="button" class="btn btn-success" name="backid" id="backid">返回</button> </td> </tr>
导入两个必备包
springMVC 的配置文件里写上
<!-- 配置文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 编码,默认编码是ISO-8859-1 --> <property name="defaultEncoding" value="UTF-8"></property> <!-- 配置允许上传文件的大小,单位字节 --> <property name="maxUploadSize" value="5848220"></property> </bean>
公共dao配置
public class BaseDao {
private static SqlSessionFactory factory;
//建立本地线程
private static ThreadLocal<SqlSession> threadLocal = new ThreadLocal<>();
//通过静态块初始化SqlSessionFactory,因为静态块只会在类第一次加载的时候执行
static {
try {
InputStream in = Resources.getResourceAsStream("Mybatis_config.xml");
//通过创建的factory对象来获取数据库的session
factory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
System.err.println("数据库连接工厂获取失败!");
}
}
/**
* 获取SqlSession对象
* @return
*/
public static SqlSession getSqlSession(){
//通过ThreadLocal获取sqlSession对象
SqlSession sqlSession = threadLocal.get();
if(sqlSession == null){
//获取一个SqlSession对象
sqlSession = factory.openSession();
//并存储到ThreadLocal对象中,通过ThreadLocal对象可以方便进行线程同步管理
threadLocal.set(sqlSession);
}
return sqlSession;
}
/**
* 关闭SqlSession对象
*/
public static void closeSqlSession(){
SqlSession sqlSession = threadLocal.get();
if(sqlSession != null){
sqlSession.close();
}
threadLocal.remove();
}
}
action里的配置
public class GoodsAction {
GoodsTypeDao goodsTypeDao = null;
GoodsDao goodsDao = null;
PageModel pageModel = new PageModel();
/**
* ajax文件上传
* @param file
* @param session
* @return
* @throws IOException
*/
@RequestMapping("/imgUpload")
@ResponseBody
public String fileUpload(@RequestParam(name="pic")MultipartFile file,HttpSession session) throws IOException{
//获取文件在服务器的存储路径
String path = session.getServletContext().getRealPath("/images/goodsDescImgs");
//获取上传文件的名称
String fileName = file.getOriginalFilename();
//进行文件存储
file.transferTo(new File(path,fileName));
return fileName;
}
goods的dao配置
public class GoodsDao extends BaseDao {
private static final String NAMESPACE_NAME = "com.carshop.mapper.GoodsMapper.";
/**
* 新增商品
* @param goods
* @return true新增成功 false新增失败
*/
public boolean saveGoods(Goods goods){
SqlSession session = getSqlSession();
int count = session.insert(NAMESPACE_NAME+"saveGoods",goods);
session.commit();
closeSqlSession();
return count>0?true:false;
}
}