谁能给我一个例子,将一个图片插入数据库。然后再将这个东西拿出来。放在页面显示。我没有做过这个东西。如果有源码的话,请发给我QQ625224672,谢谢了
解决方案 »
- 笨人新手求大神给我讲讲下面while循环,下面程序中的count为什么为3
- 关于Java IO读取自已的源文件
- 将100拆成任意个正整数的和,求最大乘积?
- 菜鸟请教个简单问题
- 散分了:改变图像的大小
- 请教一个方法,
- 请问现在用java做界面程序的人多吗?公司多吗?
- 如何取得公司内部网中, 其他机子的IP
- 客户端如何在Applet中访问服务器端的数据库,请以Access为例
- 两个表的连接问题sql="select * from user,sam_user where (+)sam_user.username=user.username";
- javassisst API
- c#转java,这段代码是什么意思呢,【急切】
google一下有挺多的呀!
http://www.diybl.com/course/4_webprogram/jsp/jsp_js/20071226/94318.html我还有一种想法,可以将图片保存在特定的位置,然后数据库设一个字段来保存图片的存放位置,呵呵,不知道可以不~测试下!
package com.yidu.group5.netbookshop.actions;import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;import com.yidu.group5.netbookshop.service.IBookService;
import com.yidu.group5.netbookshop.service.ServiceFactory;
import com.yidu.group5.netbookshop.struts.action.ActionForm;
import com.yidu.group5.netbookshop.struts.action.ActionForward;
import com.yidu.group5.netbookshop.struts.action.Actions;public class AddBookAction extends Actions { @Override
public ActionForward execute(Map<String, ActionForward> mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) {
IBookService bookService = ServiceFactory.newInstance()
.createBookService();
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
String bookName = null;
int bookTypeId = 0;
String bookPubtime = null;
String bookPubname = null;
String bookAuthor = null;
double bookPrice = 0.0;
String bookImgpath = null;
String bookIntroduce = null;
if (isMultipart) {
FileItemFactory factory = new DiskFileItemFactory();
String[] fileType = { "jpg", "jpeg", "gif", "png", "bmp", "PSD",
"tiff", "TGA" };
ServletFileUpload upload = new ServletFileUpload(factory);
Map<String, String> map = new HashMap<String, String>();
try {
List items = upload.parseRequest(request);
for (int i = 0; i < items.size(); i++) {
FileItem fileItem = (FileItem) items.get(i);
if (fileItem.isFormField()) {
map = this.processFiled(map, fileItem);
} else {
DiskFileItem dfi = (DiskFileItem) fileItem;
String filename = dfi.getName();
int x = filename.lastIndexOf(".");
String tType = null;
if (x != -1) {
tType = filename.substring(x + 1);
}
int j = 0;
for (; j < fileType.length; j++) {
if (tType.equalsIgnoreCase(fileType[j])) {
break;
}
}
if (j == fileType.length) {
request.setAttribute("insert_msg", "图片格式不对");
return mapping.get("success");
} else {
dfi.write(new File(request.getSession()
.getServletContext().getRealPath("/file")
+ System.getProperty("file.separator") + FilenameUtils.getName(dfi.getName())));
}
int z = filename.lastIndexOf("\\");
String files = null;
if (z != -1) {
files = filename.substring(z + 1);
}
bookImgpath = "file/" + files;//这里对图像路径进行了处理
}
}
} catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
bookName = map.get("name");
bookTypeId = Integer.parseInt(map.get("type"));
bookPubtime = map.get("time");
bookPubname = map.get("publishName");
bookAuthor = map.get("autor");
bookPrice = Double.parseDouble(map.get("price"));
bookIntroduce = map.get("introduction");
} boolean flag = bookService.addBook(bookName, bookTypeId, bookPubtime,
bookPubname, bookAuthor, bookPrice, bookImgpath, bookIntroduce);//这里调用service方法。把图像对象传进去,但只是传路径。
if(flag) {
request.setAttribute("insert_msg", "添加成功");
return mapping.get("success");
} else {
request.setAttribute("insert_msg", "添加失败");
return mapping.get("success");
}
} public Map<String, String> processFiled(Map<String, String> map,
FileItem fileItem) {
try {
map.put(fileItem.getFieldName(), fileItem.getString("gbk"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return map;
}}