㈠ 网页中上传图片的功能使用jsP如何实现
下一个smartUpload包。然后上传有四个步骤。//创建对象smartUpload smart = new smartUpload();//初始化上下文smart .initialize(pageContext);//开始上传smart .upload();//保存路径smart .save("image"); //要保存哪个路径中//用这个方法获取上传文件的文件名com.jspsmart.upload.File file=up.getFiles().getFile(i);这个是最基本的上传功能。文件名也不用做修改。只是传到服务器上之后用就行了。保存图片的文件名。下次直接读图片的名称就可以显示出来。
㈡ JSP如何上传图片
如果你是纯JSP写的,可以用SmartUpload.在你的页面form 里 <form action="doUpload.jsp" method="POST" enctype="multipart/form-data">文件名:<input type="text" name="name"/><br>请选择上传的文件:<input type="file" name="file1"/><input type="submit" value="上传"/></form> 注意:enctype="multipart/form-data"这个一定要这样设置,具体什么意思我也不是很清楚…..(呵呵) 提交到执行的页面如下: //实例化上传组件 SmartUpload upload = new SmartUpload(); //初始化上传组件 upload.initialize(this.getServletConfig(), request, response); //开始上传 upload.upload(); //获取上传的文件列表对象 Files f = upload.getFiles(); //获取文件对象 File fil = f.getFile(0); //去掉文件后缀 String ext = fil.getFileExt(); //判断文件类型是否是jpg格式jpg,gif,bmp,png,JPG,GIF,BMP,PNG if (!(ext.equals("jpg")) && !(ext.equals("gif")) && !(ext.equals("bmp")) && !(ext.equals("png")) && !(ext.equals("JPG")) && !(ext.equals("GIF")) && !(ext.equals("BMP")) && !(ext.equals("PNG"))) { out.println("<script type='text/javascript'>alert('文件类型错误');location.replace('upLoadPhoto.jsp');</script>"); return; } //满足条件进行文件的上传uploadImages在webRoot文件夹下的一个目录 fil.saveAs("uploadImages/" + fil.getFileName()); String filepath = "uploadImages/" + fil.getFileName(); //保存到数据库的路径 OK.这样就可以了…..
㈢ 如何用jsp上传图片到指定文件夹
String time = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());// 得到系统时间// 上传技术SmartUpload up = new SmartUpload();// 进行初始化up.initialize(this.getServletConfig(), request, response);// 开始上传try {up.upload("utf-8");//设置编码方式。int id = Integer.parseInt(up.getRequest().getParameter("id"));// 商品编号 SmartFiles sf = up.getFiles();// 得到上传的所有图片 SmartFile file = sf.getFile(0);// 根据索引得到上传图片 多个图片可以用循环:String type = file.getFileExt();// 得到图片后缀名String folder = "tp/";// 指定文件夹String path = folder + time + "." + type;// 路径 System.out.println(path + "路径");file.saveAs(request.getRealPath("/") + path);// 保存图片} catch (Exception e) {e.printStackTrace(); }//你搞个邮箱我把SmartUploadjar包 发给你吧。 //设置from提交 /*<form action="SellerServet" method="post"enctype="multipart/form-data">*/ // 加上 enctype="multipart/form-data
㈣ jsp中怎么上传图片啊
你去网上下载一个smartUpload.jar,然后把这个import到你的处理页面或者是Servlet中,例如:<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ page import="com.jspsmart.upload.*" %><%@page import="s2jsp.bysj.entity.Proct"%><%@page import="s2jsp.bysj..ProctDao"%><%@page import="s2jsp.bysj..impl.ProctDaoImpl"%><% SmartUpload su=new SmartUpload(); su.initialize(pageContext); su.upload(); int count = su.save("/image"); Request req = su.getRequest(); String serialNumber= req.getParameter("serialNumber"); String name=req.getParameter("name"); String brand=req.getParameter("brand"); String model=req.getParameter("model"); String price=req.getParameter("price"); String description=req.getParameter("description"); com.jspsmart.upload.File file = su.getFiles().getFile(0) ; String picture=file.getFileName(); Proct proct=new Proct(); proct.setSerialNumber(serialNumber); proct.setName(name); proct.setBrand(brand); proct.setModel(model); proct.setPrice(price); proct.setPicture(picture); proct.setDescription(description); ProctDao =new ProctDaoImpl(); int res=.addProct(proct); if (res!=1) { out.print("<script>alert('添加失败。');location.href='addProct.html';</script>"); return; } out.print("<script>alert('添加成功。');location.href='manageProct.jsp'</script>");%>
㈤ jsp图片上传怎么写,它涉及哪方面的知识
<form name="form1" action="/SpringDemo/login6" onsubmit="return Juge(form1);" method="post"><center><h1>班级相册</h1>选择图片: <input type="file" name="myfile" /><br><input type="submit"value="上传"/></center></form> java代码req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); //为解析类提供配置信息 DiskFileItemFactory factory = new DiskFileItemFactory(); //创建解析类的实例 ServletFileUpload sfu = new ServletFileUpload(factory); //开始解析 sfu.setFileSizeMax(1024*400); //每个表单域中数据会封装到一个对应的FileItem对象上 try { List<FileItem> items = sfu.parseRequest(req); //区分表单域 for (int i = 0; i < items.size(); i++) { FileItem item = items.get(i); //isFormField为true,表示这不是文件上传表单域 if(!item.isFormField()){ ServletContext sctx = getServletContext(); //获得存放文件的物理路径 //upload下的某个文件夹 得到当前在线的用户 找到对应的文件夹 String path = sctx.getRealPath("/upload"); System.out.println(path); //获得文件名 String fileName = item.getName(); System.out.println(fileName); //该方法在某些平台(操作系统),会返回路径+文件名 fileName = fileName.substring(fileName.lastIndexOf("/")+1); File file = new File(path+"\\"+fileName); if(!file.exists()){ item.write(file); //将上传图片的名字记录到数据库中 resp.sendRedirect("/upload/ok.html"); } } } } catch (Exception e) { e.printStackTrace(); }
㈥ Jsp上传图片到指定文件夹下 求详细代码
String time = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());// 得到系统时间 // 上传技术SmartUpload up = new SmartUpload();// 进行初始化up.initialize(this.getServletConfig(), request, response);// 开始上传try { up.upload("utf-8");//设置编码方式。int id = Integer.parseInt(up.getRequest().getParameter("id"));// 商品编号 SmartFiles sf = up.getFiles();// 得到上传的所有图片 SmartFile file = sf.getFile(0);// 根据索引得到上传图片 多个图片可以用循环:String type = file.getFileExt();// 得到图片后缀名String folder = "tp/";// 指定文件夹String path = folder + time + "." + type;// 路径 System.out.println(path + "路径"); file.saveAs(request.getRealPath("/") + path);// 保存图片} catch (Exception e) {e.printStackTrace(); }//你搞个邮箱我把SmartUploadjar包 发给你吧。 //设置from提交 /*<form action="SellerServet" method="post"enctype="multipart/form-data">*/ // 加上 enctype="multipart/form-data
㈦ 在JSP中怎样将图片上传到数据库中
到数据库?你可以建一个文件夹来保存上传的图片,然后将图片的文件名保存到数据库中。要用的时候在根据图片的文件名到该文件夹下面去读取显示出来
㈧ JSP页面上传图片到数据库怎么实现更简单
这个最好是用上传控件上传,拿到地址付给<img/>标签的src,就可以了,如果你不想用那个控件,只是要简单看下上传的是什么图片,用js获取图片上传框里的图片地址,付给<img/>标签的src也可以
㈨ jsp怎么上传图片求代码!
Apache commons-fileupload是一个很好的文件上传工具,最近使用commons-fileupload实现了图片的上传及显示,可将图片保存在指定的文件夹中,也可以将图片存放在数据库,并支持四种常用的图片格式:jpg,png,gif,bmp。 首先,跟上传一般文件一样,需要写一个servlet来处理上传的文件,你可以修改保存路径或选择将图片保存在数据库中,只需要做简单的修改就行了,servlet代码如下: FileUploadServlet.java java 代码 package com.ek.servlet; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Iterator; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.ServletFileUpload; import com.ek.image.ImageUtil; public class FileUploadServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 1L; private static String filePath = ""; /** * Destruction of the servlet. */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doPost method of the servlet. * * This method is called when a form has its tag value method equals to * post. * * @param request * the request send by the client to the server * @param response * the response send by the server to the client * @throws ServletException * if an error occurred * @throws IOException * if an error occurred */ public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html; charset=UTF-8"); DiskFileItemFactory factory = new DiskFileItemFactory(); // maximum size that will be stored in memory factory.setSizeThreshold(4096); // the location for saving data that is larger than getSizeThreshold() factory.setRepository(new File(filePath)); ServletFileUpload upload = new ServletFileUpload(factory); // maximum size before a FileUploadException will be thrown upload.setSizeMax(1000000); try { List fileItems = upload.parseRequest(req); Iterator iter = fileItems.iterator(); // Get the file name String regExp = ".+\\\\(.+\\.?())$"; Pattern fileNamePattern = Pattern.compile(regExp); while (iter.hasNext()) { FileItem item = (FileItem) iter.next(); if (!item.isFormField()) { String name = item.getName(); long size = item.getSize(); if ((name == null || name.equals("")) && size == 0) continue; Matcher m = fileNamePattern.matcher(name); boolean result = m.find(); if (result) { try { // String type = // m.group(1).substring(m.group(1).lastIndexOf('.')+1); InputStream stream = item.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] b = new byte[1000]; while (stream.read(b) > 0) { baos.write(b); } byte[] imageByte = baos.toByteArray(); String type = ImageUtil.getImageType(imageByte); if (type.equals(ImageUtil.TYPE_NOT_AVAILABLE)) throw new Exception("file is not a image"); BufferedImage myImage = ImageUtil .readImage(imageByte); // display the image ImageUtil.printImage(myImage, type, res .getOutputStream()); // save the image // if you want to save the file into database, do it here // when you want to display the image, use the method printImage in ImageUtil item.write(new File(filePath + "\\" + m.group(1))); stream.close(); baos.close(); } catch (Exception e) { e.printStackTrace(); } } else { throw new IOException("fail to upload"); } } } } catch (IOException e) { e.printStackTrace(); } catch (FileUploadException e) { e.printStackTrace(); } } /** * Initialization of the servlet. * * @throws ServletException * if an error occure */ public void init() throws ServletException { // Change the file path here filePath = getServletContext().getRealPath("/"); } 请打勾满意,原创谢谢
㈩ jsp上传图片,最好完整代码。100分!
upfile.jsp 文件代码如下:<form method="post" action="uploadimage.jsp" name="form1" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submIT" name="sub" value="upload"> </form> <form method="post" action="uploadimage.jsp" name="form1" enctype="multipart/form-data"> <input type="file" name="file"> <input type="submit" name="sub" value="upload"> </form><STRONG><FONT color=#ff0000>uploadimage.jsp</FONT></STRONG> 文件代码如下: uploadimage.jsp文件代码如下:view plain to clipboardprint?<PRE class=java name="code"><%@ page language="java" pageEncoding="gb2312"%> <%@ page import="java.io.*,java.awt.Image,java.awt.image.*,com.sun.image.codec.jpeg.*,java.sql.*,com.jspsmart.upload.*,java.util.*"%> <%@ page import="mainClass.*" %> <html> <head> <title>My JSP 'uploadimage.jsp' starting page</title> </head> <body> <% SmartUpload sma=new SmartUpload(); long file_max_size=4000000; String filename1="",ext="",testvar=""; String url="uploadfiles/"; sma.initialize(pageContext); try { sma.setAllowedFilesList("jpg,gif"); sma.upload(); }catch(Exception e){ %> <script language="jscript"> alert("只允许上传jpg,gif图片") window.location.href="upfile.jsp" </script> <% } try{ com.jspsmart.upload.File myf=sma.getFiles().getFile(0); if(myf.isMissing()){ %> <script language="jscript"> alert("请选择要上传的文件!") window.location.href="upfile.jsp" </script> <% }else{ ext=myf.getFileExt(); int file_size=myf.getSize(); String saveurl=""; if(file_size < file_max_size){ Calendar cal=Calendar.getInstance(); String filename=String.valueOf(cal.getTimeInMillis()); saveurl=request.getRealPath("/")+url; saveurl+=filename+"."+ext; myf.saveAs(saveurl,sma.SAVE_PHYSICAL); myclass mc=new myclass(request.getRealPath("data/data.mdb")); mc.executeInsert("insert into [path] values('uploadfiles/"+filename+"."+ext+"')"); out.println("图片上传成功!"); response.sendRedirect("showimg.jsp"); } } }catch(Exception e){ e.printStackTrace(); } %> </body> </html> </PRE> 本文来自: IT知道网(http://www.itwis.com) 详细出处参考:http://www.itwis.com/html/java/jsp/20080916/2409.html
未经允许不得转载:山九号 » jsp上传图片文件|在JSP中怎样将图片上传到数据库中