本来想写个controller,结果拦截器把图片拦住了,那就直接servlet

public class UploadEamge extends HttpServlet{

/**
*
*/
private static final long serialVersionUID = 1L;
private static final String[] IMAGE_TYPE = new String[] {".bmp",".jpeg",".jpg",".gif",".png"};

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 校验图片格式
DiskFileItemFactory factory = new DiskFileItemFactory();
String uploadTempDir="E:/123";
factory.setRepository(new File(uploadTempDir));
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setHeaderEncoding("utf-8");
List<FileItem> fileList;
try {
fileList = upload.parseRequest(request);
response.setCharacterEncoding("utf-8");
String uuidName=null;
Iterator<FileItem> it= fileList.iterator();
String filePath="E:/123";
File newFile = new File(filePath);
if (!newFile.exists()) {
newFile.mkdirs();// E:\all_apaches\apache-tomcat-6.0.32\apache-tomcat-6.0.32\webapps\day09_upload\img
}
while (it.hasNext()) {
FileItem item = it.next();
if (!item.isFormField()) {

String name = item.getName();
long size = item.getSize();

if (name == null || name.trim().equals("")) {
continue;
}
try {
if (size > 716800) {
response.getWriter()
.print("{\"returnMessage\":\"图片大小不能大于700K.\",\"returnCode\":\"1024\",\"imgUrl\":\"\"}");

}
} catch (Exception e) {
e.getStackTrace();
}
boolean isLegal = false;
for (String type : IMAGE_TYPE) {
if (StringUtils.endsWithIgnoreCase(item.getName(), type)) {
isLegal = true;
System.out.println(isLegal);
break;
}
System.out.println(isLegal);
}

try {
uuidName=generateRandonFileName(item.getName());
item.write(new File(newFile, uuidName));
item.delete();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}

String url = "http://image.brand.com"+ "/"+uuidName;
System.out.println(url);
response.getWriter().print(
"{\"returnMessage\":\"\",\"returnCode\":\"0000\",\"imgUrl\":\""
+ url + "\"}");

}

}catch (Exception e) {
e.getStackTrace();
}

}
public static String generateRandonFileName(String fileName) {
String ext = fileName.substring(fileName.lastIndexOf("."));
return UUID.randomUUID().toString() + ext;
}

}

一个图片上传的servlet,传到本地磁盘,要传到服务器请修改的更多相关文章

  1. 我需要在Web上完成一个图片上传的功能

    我需要在Web上完成一个图片上传的功能. 这个页面需要能从手机中选择图片上传. 首先,这个页面是从微信上面触发的,所以修改了微信的的入口地址,增加了身份识别号作为传参. 跳转到页面的时候,页面先检查身 ...

  2. VUE2.0+VUE-Router做一个图片上传预览的组件

    之前发了一篇关于自己看待前端组件化的文章,但是由于学习和实践的业务逻辑差异,所以自己练习的一些demo逻辑比较简单,打算用vue重构现在公司做的项目,所以在一些小的功能页面上使用vue来做的,现在写的 ...

  3. 分享一个图片上传插件(TP5.0)

    效果预览图: 该插件主要功能是:可预览裁剪图片和保存原图片,执行裁剪图片后会删除 裁剪的原图片目录,以便减少空间.一.下载附件 地址:https://pan.baidu.com/s/1bpxZhM3 ...

  4. 简单的做一个图片上传预览(web前端)

    转载:点击查看原文 在做web项目很多的时候图片都是避免不了的,所以操作图片就成了一个相对比较棘手的问题,其实也不是说很麻烦,只是说上传然后直接预览的过程很恶心,今天简单的做一个处理. 效果预览: & ...

  5. Flex + .Net从本地选择一个图片上传到服务器

    <mx:TextInput id="TxtFileName" editable="false" width="200"/> &l ...

  6. 我需要在Web上完成一个图片上传的功能(+2)

    增加一个页面,用于判断传参是否正确. @{     //判断是否具备会员参数     if (UrlData.Count > 0)     {         Session["Arg ...

  7. 我需要在Web上完成一个图片上传的功能后续(+1)

    微信入口施工完成.关键字识别中增加了本次活动的"关键字",在系统中增加了链接.不过,由于地址包含私密关键参数,这里隐藏,敬请原谅. 下一步,微信链接的地址页面是要对微信用户的信息进 ...

  8. 使用Struts2实现图片上传和拦截器

    今天来分享一个图片上传 现在很多小项目里面基本上都有要显示图片的功能,所以呢图片上传是基本要掌握的啦 一般的图片上传原理就是从本地选择一张图片然后通过io流发布到服务器上去 上传方案基本有三种: 1. ...

  9. HTML5笔记:跨域通讯、多线程、本地存储和多图片上传技术

    最近做项目在前端我使用了很多新技术,这些技术有bootstrap.angularjs,不过最让我兴奋的还是使用了HTML5的技术,今天我想总结一些HTML5的技术,好记性不如烂笔头,写写文章可以很好的 ...

随机推荐

  1. Xtrabackup 对MYSQL进行备份还原

    在操作MYSQL中注意两个概念: 干什么都记得 flush privileges; grant all on *.* to root@'localhost' identified by 'passwo ...

  2. [LeetCode 120] - 三角形(Triangle)

    问题 给出一个三角形,找出从顶部至底部的最小路径和.每一步你只能移动到下一行的邻接数字. 例如,给出如下三角形: [ [2], [3,4], [6,5,7], [4,1,8,3] ] 从顶部至底部的最 ...

  3. Android新浪微博客户端(一)——主框架搭建

    原文出自:方杰| http://fangjie.info/?p=62 转载请注明出处 提前声明的是,我是按照Ivan的这套教程学下来的. 首先,对于任何应用我们都需要建立一套消息处理机制,就是当用户在 ...

  4. ubuntu下,thinkphp验证码不能用

    首先安装gd库 使用apt-get方式为Ubuntu安装PHP+MYSQL+Apache 默认是没有GD库的 1.安装gd库 命令:sudo apt-get install php5-gd 2.给权限 ...

  5. iOS UIView指定显示摸一个角弧形显示

    关键代码: UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect: view.bounds byRoundingCorner ...

  6. C++11: final与override

    C++11中增加了final与override关键字,貌似是从Java语言中借鉴而来,用途也一样.看例子代码: 01.#include <iostream> 02.  03.using n ...

  7. ArrayList 学习笔记

        接口   ArrayList实现了List接口,因此可以当作一个List来使用. 此外,ArrayList还实现RandomAccess接口和Serializable,说明ArrayList支 ...

  8. Shell-WEB目录监控

    #!/bin/sh #date:2015-12-08 #filename:check_webfile.sh #作者:李兴利 #Email:1162572407@qq.com #version:v1.1 ...

  9. canvas-画蜗牛

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  10. Function.prototype.bind

    解析Function.prototype.bind 简介 对于一个给定的函数,创造一个绑定对象的新函数,这个函数和之前的函数功能一样,this值是它的第一个参数,其它参数,作为新的函数的给定参数. b ...