日期:2014-05-18  浏览次数:20770 次

!!关于jsp图片上传和加载的一些重要问题!!急!!在线等!!跪求!!
怎么样在本地的文件夹中选择图片加载到jsp页面中?然后怎么样把图片存入数据库(sqlserver2000),还需要在查询的时候查询出这个图片?  
注:是不是需要先把本地图片存入到服务器的临时文件夹下?  
有没有参考的代码,谢谢各位大虾~~~跪求!!!  


------解决方案--------------------
JSP页面
<%@page contentType= "text/html;charset=GBK "%>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-logic "
prefix= "logic "%>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-html "
prefix= "html "%>
<%@ taglib uri= "http://jakarta.apache.org/struts/tags-bean "
prefix= "bean "%>
<html>
<script type= "text/javascript ">
function v(){

document.myform.img.src=document.myform.pic.value;
}

</script>
<body>
<form action= "photo.do?p=main " method= "POST " enctype= "multipart/form-data " name= "myform "> <%--
得到本地的图片
--%> <input type= "file " name= "pic " onchange= "v() " >
<img name= "img " src= " " > <%--

显示数据库中的图片
--%> <img name= "abc " src= "photo.do?p=view ">
<input type= "submit " value= "going "/>
</form>
</body>
</html>
Action
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;

import form.PhotoForm;

/**
* MyEclipse Struts Creation date: 04-13-2007
*
* XDoclet definition:
*
* @struts.action validate= "true "
*/
public class PhotoAction extends DispatchAction {
/*
* Generated Methods
*/

/**
* Method execute
*
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward main(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// TODO Auto-generated method stub

PhotoForm tform = (PhotoForm) form;

FormFile file = tform.getPic();
String name = "ruby ";
HttpSession session = request.getSession();
if (file.getFileSize() > 10000) {

}
// 获取文件名
String filename = file.getFileName();
System.out.println(filename);
int index = filename.lastIndexOf( &qu