Apache图片服务器 ,图片上传

路口xia车 2015-05-06 04:17:00
Apache 作为网站图片服务器,上传是如何处理的。直接用 http: RRL 形式上传能行否?还是要用ftp上传图片!请多指教!
...全文
391 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
engourdi 2015-05-11
  • 打赏
  • 举报
回复
为何不用第三方的,诸如七牛之类的。自己运维成本太高,还的做cdn
Stay_alon 2015-05-11
  • 打赏
  • 举报
回复
用apache的组件包 fileupload.jar
  • 打赏
  • 举报
回复
java IO流
liuhongce 2015-05-07
  • 打赏
  • 举报
回复
这个上传首先要用到两个来源包,一个io,一个fileupload,然后有两种方法,首先你要不用struts2框架的话就这样: public class UploadServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } /** * read方法一次只读一个字节,read(byte[] b)也是读取一个字节,只不过要将字节放入到byte[]中 */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //4.获得临时文件的路径 String tempPath = this.getServletContext().getRealPath("/temp"); System.out.println(tempPath); //获得下载文件的路径 String uploadPath = this.getServletContext().getRealPath("/fileupload"); System.out.println(uploadPath); //1.获得工厂 DiskFileItemFactory factory = new DiskFileItemFactory(); //3.设置缓冲区大小 factory.setSizeThreshold(10000*1024); //5.设置临时文件(其存放的位置) factory.setRepository(new File(tempPath)); //2.获得ServletFileUpload对象,此处并没有通过静态方法获得,有些疑惑 ServletFileUpload upload = new ServletFileUpload(factory); String message = null; //设置文件名中文乱码 upload.setHeaderEncoding("utf8"); //6.获得FileItem对象的list集合 try { List<FileItem> items = upload.parseRequest(request); for(FileItem item : items){ if(item.isFormField()){ //普通字段 System.out.println(item.getFieldName()); System.out.println(item.getString()); }else{ //上传字段 InputStream ins = item.getInputStream(); //设置下载文件的绝对路径 String fileName = item.getName(); //去掉文件名的前缀,fileName的前缀,截取文件名 int index = fileName.lastIndexOf("\\"); if(index>0){ fileName = fileName.substring(index); } //获取UUID字符串,添加到文件名之前,可以保证文件名不相同 String uuid = UUID.randomUUID().toString(); File file = new File(uploadPath+"\\"+uuid+"_"+fileName); OutputStream out = new FileOutputStream(file); byte[] b = new byte[1024]; int step = 0; while((step=ins.read(b))!= -1){ out.write(b); } ins.close(); out.close(); message = "保存成功"; } } } catch (FileUploadException e) { message = "保存失败"; e.printStackTrace(); }finally{ request.setAttribute("message", message); request.getRequestDispatcher("/upload/result.jsp").forward(request, response); } } } 当然如果你用struts2会很简单,只需要定义3个参数,然后提供setget方法,最后核心方法这样: public String saveFile() throws Exception{ System.out.println("UploadAction.saveFile()"); //实现上传 //设置上传文件的路径 String uploadPath = ServletActionContext.getServletContext().getRealPath("upload"); uploadPath = uploadPath + "/" +uploadImageFileName; File uploadFile = new File(uploadPath); //拷贝文件 FileUtils.copyFile(uploadImage, uploadFile); return SUCCESS; } 这样就搞定了啊...当然用modeldriven更简单,不多说了就,给我点分让我擦擦汗吧,,☺
路口xia车 2015-05-06
  • 打赏
  • 举报
回复

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧