资源预览内容
第1页 / 共7页
第2页 / 共7页
第3页 / 共7页
第4页 / 共7页
第5页 / 共7页
第6页 / 共7页
第7页 / 共7页
亲,该文档总共7页全部预览完了,如果喜欢就下载吧!
资源描述
JavaJava 上传图片到服务器上传图片到服务器 j jJava 上传图片到服务器 java-base 2010-01-12 16:44:09 阅读1293 评论 0 字号:大中小 订阅 .对于图片处理有两种: 一种是插入数据库;一种是上传到服务器上。本次讨论后一种 情况。关于图片的访问,不要通过 app 服务器访问,即图片不要放在app 服务器上,这样下去图片占的内存会越来越大,迟早会把 app服务器搞死,针对的是 java 的开源的 tomcat 服务器图片的访问应该通过 apache 及其他的一些 web server 来访问。Jsp+Servlet upload fileservlet 文件import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.Date;import java.util.Iterator;import java.util.List;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.DefaultFileItemFactory;import org.apache.commons.fileupload.DiskFileUpload;import org.apache.commons.fileupload.FileItem;import org.apache.commons.fileupload.FileItemFactory;import org.apache.commons.fileupload.FileItemIterator;import org.apache.commons.fileupload.FileItemStream;import org.apache.commons.fileupload.FileUploadException;import org.apache.commons.fileupload.disk.DiskFileItemFactory;import org.apache.commons.fileupload.servlet.ServletFileUpload;import org.apache.commons.fileupload.util.Streams;/* Servlet implementation class for Servlet: UploadServlet*/public class UploadServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet File tmpDir = null;/初始化上传文件的临时存放目录File saveDir = null;/初始化上传文件后的保存目录public UploadServlet() super(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException this.doPost(request,response);protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException tryString pp=null;String upto=null;if(ServletFileUpload.isMultipartContent(request)DiskFileItemFactory dff = new DiskFileItemFactory();/创建该对象dff.setRepository(tmpDir);/指定上传文件的临时目录dff.setSizeThreshold(10240000);/指定在内存中缓存数据大小,单位为 byteServletFileUpload sfu = new ServletFileUpload(dff);/创建该对象sfu.setFileSizeMax(50000000);/指定单个上传文件的最大尺寸sfu.setSizeMax(10000000);/指定一次上传多个文件的总尺寸FileItemIterator fii = sfu.getItemIterator(request);/解析request 请求,并返回 FileItemIterator 集合while(fii.hasNext()FileItemStream fis = fii.next();/从集合中获得一个文件流if(!fis.isFormField() /获得上传文件的文件名System.out.println(“request.getRealPath()=“+request.getRealPath(“/“);String uploadPath = request.getRealPath(“/“)+“dataimages“;/选定上传的目录此处为当前目录if(!new File(uploadPath).isDirectory()/选定上传的目录此处为当前目录,没有则创建new File(uploadPath).mkdirs();System.out.println(“uploadPath=“+uploadPath);fileName=fileName.substring(fileName.lastIndexOf(“.“);/获取从.开始到最后的字符/将时间转化为字符串用于给文件或者文件夹改名,防止传上来的图片名称相同Date time=new Date();String dirTime=String.valueOf(time.getTime();/BufferedInputStream in = new BufferedInputStream(fis.openStream();/获得文件输入流/BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(saveDir+“+dirTime+fileName);/获得文件输出流BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(uploadPath+“+dirTime+fileName);/获得文件输出流/pp 为已经上传的文件pp=uploadPath+dirTime+fileName;System.out.println(“pp=“+pp);/upto 为上传的路径upto=uploadPath;Streams.copy(in, out, true);/开始把文件写到你指定的上传文件夹/*/定义解压字符串,用于解压上传的 rar 文件,注意此处需要一个 winrar.exe 文件String jieya=request.getRealPath(“/“)+ “WinRAR.exe x -t -o+ -p- “+pp+“ “+upto+“;/String jieya=“D:Tomcat 5.5webappsfileuploadWinRAR.exe x -t -o+ -p- “+pp+“ “+upto+“;Process p=Runtime.getRuntime().exec(jieya);/将传输的 rar 文件解压*/response.getWriter().println(“File upload successfully!“);/终于成功了,还不到你的上传文件中看看,你要的东西都到齐了吗catch(Exception e)e.printStackTrace(); public void init() throws ServletException /初始化,没什么意义super.init();String tmpPath = “c:tmpdir“;String savePath = “c:updir“;tmpDir = new File(tmpPath);saveDir = new File(savePath);if(!tmpDir.isDirectory()tmpDir.mkdir();if(!saveDir.isDirectory()saveDir.mkdir(); web.xml 文件UploadServletdd.UploadServletUploadServlet/UploadServlet
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号