上传解决方案之一:上传处理大数据blob
uploadify+hibernate+ oracle
代码应该与mysql mssqlserver 通用 本人用的是oracle 其它没有测试
本文章专提供给使用uploadify 上传大文件处理的问题,前提是假设看本文档的人已经会使用uploadify 上文件 如果还不会使用的请看本人的uploadify 教程
1.Learner.java code
public class LearnerInfo implements java.io.Serializable {
/
private String learnerId;//学员ID
private String learnerName;//学员姓名
private byte[] photo;
// Constructors
/** default constructor */
public Learner() {
}
public String getLearnerId() {
return this.learnerId;
}
public void setLearnerId(String learnerId) {
this.learnerId = learnerId;
}
public String getLearnerName() {
return this.learnerName;
}
public void setLearnerName(String learnerName) {
this.learnerName = learnerName;
}
/**
* @return the photo
*/
public byte[] getPhoto() {
return photo;
}
/**
* @param photo the photo to set
*/
public void setPhoto(byte[] photo) {
this.photo = photo;
}
}
2.learner.xml code
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Learner" table="LEARNER">
<id name="learnerId" type="java.lang.String">
<column name="LEARNER_ID" length="8" />
<generator class="assigned"></generator>
</id>
<property name="learnerName" type="java.lang.String">
<column name="LEARNER_NAME" length="30">
</column>
</property>
<property name="photo" type="org.springframework.orm.hibernate3.support.BlobByteArrayType">
<column name="PHOTO" >
</column>
</property>
</class>
</hibernate-mapping>
3上传java code
public void uploadImg(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建磁盘文件工厂
DiskFileItemFactory fac = new DiskFileItemFactory();
// 设置内存缓冲区,超过后写入临时文件
fac.setSizeThreshold(20240000);
// 创建servlet文件上传组件
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("UTF-8");
// 遍历从前台得到的文件列表
List<?> items = null;
FileItem item = null;
try {
items = upload.parseRequest(request);
for (int i = 0; i < items.size(); i++) {
item = (FileItem) items.get(i);
//fileName = "D:" + File.separator + item.getName();
// 保存文件
if (!item.isFormField() && item.getName().length() > 0) {
tempName = item.getName();
byte[] buffer = item.get();
Learner learner = new Learner();
learner.setLearnerId(“1”);
learner.setLearnerName(“学生姓名”);
learner.setPhoto(buffer );
Learnerbs.save(learner)
}
}
} catch (FileUploadException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("coachId", coachId);
request.setAttribute("learnerId", learnerId);
//response.sendRedirect("success.jsp");
//request.getRequestDispatcher("success.jsp").forward(request,response);
// 打印输出
PrintWriter out = null;
out = encodehead(request, response);
out.write("上传成功!");
out.close();
}
4.展示图片 java code
public void displayPhoto(){
String id = request.getParameter("id");
Learner learner =LearnerBS.get(id);
byte[] photo = learner.getPhoto();
if(photo==null){
return;
}
InputStream is = null;
try {
is = new ByteArrayInputStream(photo);
byte[] buffer = new byte[1024];
int len = 0;
OutputStream out = response().getOutputStream();
while((len=is.read(buffer))>0){
out.write(buffer,0, len);
}
out.close();
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
uploadify+hibernate+ oracle
代码应该与mysql mssqlserver 通用 本人用的是oracle 其它没有测试
本文章专提供给使用uploadify 上传大文件处理的问题,前提是假设看本文档的人已经会使用uploadify 上文件 如果还不会使用的请看本人的uploadify 教程
1.Learner.java code
public class LearnerInfo implements java.io.Serializable {
/
private String learnerId;//学员ID
private String learnerName;//学员姓名
private byte[] photo;
// Constructors
/** default constructor */
public Learner() {
}
public String getLearnerId() {
return this.learnerId;
}
public void setLearnerId(String learnerId) {
this.learnerId = learnerId;
}
public String getLearnerName() {
return this.learnerName;
}
public void setLearnerName(String learnerName) {
this.learnerName = learnerName;
}
/**
* @return the photo
*/
public byte[] getPhoto() {
return photo;
}
/**
* @param photo the photo to set
*/
public void setPhoto(byte[] photo) {
this.photo = photo;
}
}
2.learner.xml code
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Learner" table="LEARNER">
<id name="learnerId" type="java.lang.String">
<column name="LEARNER_ID" length="8" />
<generator class="assigned"></generator>
</id>
<property name="learnerName" type="java.lang.String">
<column name="LEARNER_NAME" length="30">
</column>
</property>
<property name="photo" type="org.springframework.orm.hibernate3.support.BlobByteArrayType">
<column name="PHOTO" >
</column>
</property>
</class>
</hibernate-mapping>
3上传java code
public void uploadImg(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建磁盘文件工厂
DiskFileItemFactory fac = new DiskFileItemFactory();
// 设置内存缓冲区,超过后写入临时文件
fac.setSizeThreshold(20240000);
// 创建servlet文件上传组件
ServletFileUpload upload = new ServletFileUpload(fac);
upload.setHeaderEncoding("UTF-8");
// 遍历从前台得到的文件列表
List<?> items = null;
FileItem item = null;
try {
items = upload.parseRequest(request);
for (int i = 0; i < items.size(); i++) {
item = (FileItem) items.get(i);
//fileName = "D:" + File.separator + item.getName();
// 保存文件
if (!item.isFormField() && item.getName().length() > 0) {
tempName = item.getName();
byte[] buffer = item.get();
Learner learner = new Learner();
learner.setLearnerId(“1”);
learner.setLearnerName(“学生姓名”);
learner.setPhoto(buffer );
Learnerbs.save(learner)
}
}
} catch (FileUploadException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("coachId", coachId);
request.setAttribute("learnerId", learnerId);
//response.sendRedirect("success.jsp");
//request.getRequestDispatcher("success.jsp").forward(request,response);
// 打印输出
PrintWriter out = null;
out = encodehead(request, response);
out.write("上传成功!");
out.close();
}
4.展示图片 java code
public void displayPhoto(){
String id = request.getParameter("id");
Learner learner =LearnerBS.get(id);
byte[] photo = learner.getPhoto();
if(photo==null){
return;
}
InputStream is = null;
try {
is = new ByteArrayInputStream(photo);
byte[] buffer = new byte[1024];
int len = 0;
OutputStream out = response().getOutputStream();
while((len=is.read(buffer))>0){
out.write(buffer,0, len);
}
out.close();
out.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}