急!求java上传图片代码!
本人用ssh技术在做一个网站,需要后台管理新闻的软件上传图片,显示在前台页面上,新手,刚上任不久,请高手们指点!!小女子先谢了!
需要达到的效果:
1.上传的图片保存在发布的工程目录下,图片地址保存到数据库。
2.到数据库查询图片路径,显示图片。
代码被采用者大大的给分!!不要说用google...
------解决方案--------------------upload.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,com.jspsmart.upload.*" %>  
<html>  
<head>  
<title>上传文件 </title>  
</head>  
<body>  
<form id="form1" name="msform" method="post" action="do_upload.jsp" enctype="multipart/form-data" onSubmit="return Check_Found(this);" target="iframe1">  
<table width="50%" border="1" align="center">  
 <tr>  
   <td align="center"><input type="text" name="name" id="text1"></td>  
 </tr>  
 <tr>  
   <td align="center">产品说明:  
     <input type="file" name="file2" value=""/>  
	  <iframe name="iframe1" style="display:none"> </iframe>  
     <input type="submit" name="Submit" value="上传图片" />  
	</td>  
 </tr>  
</table>  
</form>  
</body>  
</html>  
do_upload.jsp
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,com.jspsmart.upload.*" %>  
<html>  
<head>  
<title>文件上传处理页面 </title>  
</head>  
<body>  
<%  
	SmartUpload su=new SmartUpload();  
	su.initialize(pageContext);  
	su.upload();  
	String name;  
	int count=su.save("/upload",su.SAVE_VIRTUAL);  
	out.println(count+"个文件上传成功! <br>");  
	for(int i=0;i <su.getFiles().getCount();i++)  
	{  
		com.jspsmart.upload.File file=su.getFiles().getFile(i);  
		if(file.isMissing()) continue;  
		String files=file.getFileName();  
		out.print("<script>window.parent.document.text1.value='../upload/"+file.getFileName()+"';</script>");
		out.print("<script>alert('上传成功!');</script>");
		response.setHeader("Refresh","0;URL=addnewproduce.jsp");
	}  
%>  
</body>  
</html>  
还需要组件才行
------解决方案--------------------
//<input type="file" id="upload" name="upload" style="width:100%"/>
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import 
java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
  /**
 * @param args
 *            文件字节输入、输出流方法拷贝一个文件到指定目录
 */
  void copy(File f1, File f2) {//f1 源文件路径  f2目标路径   最后把f2 insert 到数据库就不写了
 try {
  // 建立相关的字节输入流
  FileInputStream fr = new FileInputStream(f1); // 通过打开一个到实际文件的连接来创建一个
              // FileInputStream,该文件通过文件系统中的路径名             
  // 创建一个向具有指定名称的文件中写入数据的输出文件流。
  FileOutputStream fw = new FileOutputStream(f2);
  byte buffer[] = new byte[1]; // 声明一个byte型的数组,数组的大小是512个字节
  while (fr.read(buffer) != -1) {