package com.mvc.controller.teacher;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import com.mvc.entity.FileManager;
import com.mvc.service.FileService;
/**
*
* 文件上传控制类FileController
* @author 杜维奥
* 2012-12-27
*
* */
@Controller
@RequestMapping("/mayuyu.do")
public class FileController {
//得到日志
protected final transient Log log = LogFactory
.getLog( FileController.class);
//Autowired这个注解就是spring可以自动帮你
//把bean里面引用的对象的setter/getter方法省略
@Autowired
private FileService fileService;
//@RequestMapping
//无参数,默认调用方法
//TODO
//上传文件ModelAndView
@RequestMapping(params = "method=fileUpload",method = RequestMethod.POST)
public String fileUpload(HttpServletRequest request,
ModelMap modelMap) throws Exception{
try{
MultipartHttpServletRequest multipartRequest =
(MultipartHttpServletRequest)request;
//得到图片内容
//获得文件
CommonsMultipartFile file =
(CommonsMultipartFile) multipartRequest.getFile("filePic");
//得到图片参数
String name =multipartRequest.getParameter("name");
System.out.println("filename:"+name);
//得到文件名
String realFileName = file.getOriginalFilename();
System.out.println("realFileName:"+realFileName);
//获取存放路径
//String picSavePath = request.getSession()
String picSavePath = request.getSession()
.getServletContext().getRealPath("/file/pic");
System.out.println("picSavePath:"+picSavePath);
//创建文件
File dirPath = new File(picSavePath);
//如不存在
if(!dirPath.exists()){
//路径+文件创建
dirPath.mkdir();
}
//本地文件
File upLoadFile = new File(picSavePath+"/"+realFileName);
//由Spring提供文件复制工具
FileCopyUtils.copy(file.getBytes(), upLoadFile);
//得到图片路径
String picPath = upLoadFile.getAbsolutePath();
System.out.println(picPath);
//主要是前台的显示
modelMap.put("picPath", picPath);
}
catch(Exception e){
//log.error(e.getMessage());
//modelMap.put("picPath", "不是图片格式");
e.printStackTrace();
}
return "pic";
//return new ModelAndView("redirect:/file/pic.jsp");
}
//上传文件---转成二进制流
@RequestMapping(params = "method=UploadOracle",method = RequestMethod.POST)
public String UploadOracle(HttpServletRequest request,
ModelMap modelMap) throws Exception{
try{
MultipartHttpServletRequest multipartRequest =
(MultipartHttpServletRequest)request;
//得到图片内容
//获得文件
CommonsMultipartFile file =
(CommonsMultipartFile) multipartRequest.getFile("filePic");
//得到是流字节
byte[] data = file.getBytes();
//存到Oracle
//调用接口
int id = fileService.FileUpload(data, file.getOriginalFilename());
modelMap.put("FileID", id);
System.out.println("编号:"+id);
}
catch(Exception e){
//log.error(e.getMessage());
//modelMap.put("picPath", "不是图片格式");
e.printStackTrace();
}
return "picDataBase";
//return new ModelAndView("redirect:/file/pic.jsp");
}
//解析图片
@RequestMapping(params = "method=showPic")
public void showPic(@RequestParam("FileID")int id,HttpServletResponse response) {
//第一种方法
try{
System.out.println(id);
FileManager obj=(FileManager) fileService.find(id);
System.out.println(obj.getFileName());
ServletOutputStream outputStream=response.getOutputStream();
response.reset();
response.setContentType("image/jpeg");
try {
outputStream.write(obj.getFileByte(), 0, obj.getFileByte().length);
outputStream.flush();
} finally {
if (outputStream != null) {
outputStream.close();
}
}
}catch(IOException e){
e.printStackTrace();
}
//第二种方法
//主要用于SESSION
//http://blog.csdn.net/yincheng886337/article/details/6830341
// FileManager obj=(FileManager) fileService.find(id);
// InputStream in = new ByteArrayInputStream(obj.getFileByte());
// try {
// ImageIO.write((RenderedImage) in, "image/jpeg", response.getOutputStream());
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
springMvc+Oracle的图片转二进制流
共83个文件
jar:40个
class:10个
jsp:9个
4星 · 超过85%的资源 需积分: 10 40 下载量 65 浏览量
2013-06-24
15:08:10
上传
评论
收藏 15.12MB ZIP 举报
温馨提示
springMvc+Oracle的图片转二进制流,导入Eclipse中,sql文件在WebRoot中。添加数据表后,更改hibernate.properties配置文件。然后再运行。
资源推荐
资源详情
资源评论
收起资源包目录
springfile.zip (83个子文件)
springfile
.project 1KB
WebContent
sql
mvc.sql 1KB
WEB-INF
view
pic.jsp 710B
student_update.jsp 1KB
student_add.jsp 1013B
student
student.jsp 2KB
picDataBase.jsp 945B
spring-servlet.xml 2KB
tmp
classes
lib
servlet-api.jar 86KB
org.springframework.beans-3.0.5.RELEASE.jar 542KB
cglib-2.2.jar 272KB
log4j-1.2.16.jar 470KB
org.springframework.expression-3.0.5.RELEASE.jar 166KB
aopalliance-1.0.0.jar 5KB
dom4j-1.6.1.jar 307KB
hsqldb.jar 629KB
org.springframework.context.support-3.0.5.RELEASE.jar 99KB
org.springframework.aspects-3.0.5.RELEASE.jar 35KB
jstl-1.2.jar 405KB
新建文件夹
javax.persistence.jar 131KB
slf4j-log4j12-1.6.1.jar 10KB
commons-io-1.4.0.jar 108KB
org.springframework.asm-3.0.5.RELEASE.jar 52KB
jtds-1.2.4.jar 298KB
javax.transaction.jar 23KB
org.springframework.transaction-3.0.5.RELEASE.jar 226KB
org.springframework.aop-3.0.5.RELEASE.jar 314KB
antlr-2.7.6.jar 433KB
aspectjrt-1.6.8.jar 113KB
org.springframework.oxm-3.0.5.RELEASE.jar 60KB
commons-fileupload.jar 52KB
slf4j-api-1.6.1.jar 25KB
org.springframework.web.servlet-3.0.5.RELEASE.jar 409KB
commons-logging.jar 59KB
asm-all-3.2.jar 203KB
org.springframework.orm-3.0.5.RELEASE.jar 326KB
hibernate3.jar 3.75MB
org.springframework.web-3.0.5.RELEASE.jar 386KB
aspectjweaver-1.6.8.jar 1.55MB
org.springframework.jms-3.0.5.RELEASE.jar 181KB
org.springframework.context-3.0.5.RELEASE.jar 653KB
org.springframework.core-3.0.5.RELEASE.jar 373KB
org.springframework.jdbc-3.0.5.RELEASE.jar 377KB
hibernate-validator-4.1.0.Final.jar 281KB
ojdbc14.jar 1.47MB
mysql-connector-java-5.1.12-bin.jar 716KB
commons-collections-3.2.1.jar 562KB
javassist-3.12.0.GA.jar 618KB
work
web.xml 2KB
index.jsp 416B
script
jquery.min.js 76KB
META-INF
MANIFEST.MF 39B
file
pic
mayu_fileUpload.jsp 898B
mayu_fileUpload.jsp 1KB
include
head.jsp 230B
src
applicationContext.xml 3KB
com
mvc
service
FileService.java 1KB
controller
teacher
FileController.java 5KB
dao
EntityDao.java 318B
EntityDaoImpl.java 2KB
entity
FileManager.java 1KB
hibernate.properties 699B
log4j.properties 229B
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 460B
.jsdtscope 503B
build
classes
applicationContext.xml 3KB
com
mvc
service
FileService.class 2KB
controller
teacher
FileController.class 5KB
dao
EntityDaoImpl$4.class 1KB
EntityDaoImpl$1.class 1KB
EntityDaoImpl.class 2KB
EntityDaoImpl$5.class 1KB
EntityDaoImpl$2.class 1KB
EntityDao.class 421B
EntityDaoImpl$3.class 1KB
entity
FileManager.class 2KB
hibernate.properties 699B
log4j.properties 229B
.classpath 834B
共 83 条
- 1
资源评论
- 我不够好i2018-01-19还不错,只是要详细点就好还不错,只是要详细点就好
- osanqier2015-05-21还不错,只是要详细点就好
- lastzhutou2019-05-24要详细点就好
lyknz
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功