《java上传图片到服务器j》由会员分享,可在线阅读,更多相关《java上传图片到服务器j(7页珍藏版)》请在金锄头文库上搜索。
1、JavaJava 上传图片到服务器上传图片到服务器 j jJava 上传图片到服务器 java-base 2010-01-12 16:44:09 阅读1293 评论 0 字号:大中小 订阅 .对于图片处理有两种: 一种是插入数据库;一种是上传到服务器上。本次讨论后一种 情况。关于图片的访问,不要通过 app 服务器访问,即图片不要放在app 服务器上,这样下去图片占的内存会越来越大,迟早会把 app服务器搞死,针对的是 java 的开源的 tomcat 服务器图片的访问应该通过 apache 及其他的一些 web server 来访问。Jsp+Servlet upload fileservle
2、t 文件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.HttpServletR
3、equest;import javax.servlet.http.HttpServletResponse;import mons.fileupload.DefaultFileItemFactory;import mons.fileupload.DiskFileUpload;import mons.fileupload.FileItem;import mons.fileupload.FileItemFactory;import mons.fileupload.FileItemIterator;import mons.fileupload.FileItemStream;import mons.fi
4、leupload.FileUploadException;import mons.fileupload.disk.DiskFileItemFactory;import mons.fileupload.servlet.ServletFileUpload;import mons.fileupload.util.Streams;/* Servlet implementation class for Servlet: UploadServlet*/public class UploadServlet extends javax.servlet.http.HttpServlet implements j
5、avax.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(HttpServlet
6、Request 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);/指定在内存
7、中缓存数据大小,单位为 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();/从集合
8、中获得一个文件流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(“uploadPa
9、th=“+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 Buffered
10、OutputStream(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;
11、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);
12、/将传输的 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