1、接收到的是图片的流时

//上传头像
@RequestMapping(value = "/uploadHeadSculpture", method = RequestMethod.POST)
@ResponseBody
public String uploadHeadSculpture(@RequestParam("photo") String file) {
User user = (User) SecurityUtils.getSubject().getSession().getAttribute("curr_user");
//获取文件格式
String postfix = file.split("/")[1].split(";")[0];
//获取图片的Base64码
String str = file.split(",")[1];
String url = "";
BASE64Decoder decoder = new BASE64Decoder();
try {
// Base64解码
byte[] bytes = decoder.decodeBuffer(str);
for (int i = 0; i < bytes.length; ++i) {
// 调整异常数据
if (bytes[i] < 0) {
bytes[i] += 256;
}
}
long title = Calendar.getInstance().getTimeInMillis();
//获取系统路径并设定文件保存的目录
String dir = ServiceConfigUtil.getValue("imgpath");//图片的上传路径,我这里是从工程的配置文件获取的
String fileName = title + "." + postfix;
// 生成jpeg图片
FileUtils.writeByteArrayToFile(new File(dir, fileName), bytes);
String lookUserPhoto = ServiceConfigUtil.getValue("lookUserPhoto");//图片的访问路径,我这里是从工程配置文件获取的,可以自己定义。如果你的图片保存在工程目录下,可以直接用dir+fileName
url = lookUserPhoto + fileName;//保存到数据库的图片访问路径
/××
        ×保存url到数据库
       ××/
} catch (Exception e) {return "no";
}return "yes";
}

注:接收参数file值的一个基本格式

  "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAYAAADDPmHLAAAdiUlEQVR........."

2、接收到的是file文件直接上传

 @RequestMapping(value={"/saveOrUpdate"},method=RequestMethod.POST)
public String saveOrUpdate(Person p, @RequestParam("photo") MultipartFile file, HttpServletRequest request) throws IOException{
if(!file.isEmpty()){
ServletContext sc = request.getSession().getServletContext();
String dir = sc.getRealPath(“/upload”); //设定文件保存的目录 String filename = file.getOriginalFilename(); //得到上传时的文件名
FileUtils.writeByteArrayToFile(new File(dir,filename), file.getBytes()); p.setPhotoPath(“/upload/”+filename); //设置图片所在路径 System.out.println("upload over. "+ filename);
}
ps.saveOrUpdate(p);
return "redirect:/person/list.action"; //重定向
}

2.1、页面

<form action="/saveOrUpdate" enctype="multipart/form-data" method="post">
<input type="file" name="photo">
<input type="submit" value="commit">
</form>

2.2、需要在springmvc配置文件中添加

<!--上传文件-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000000000"/>
<property name="defaultEncoding" value="UTF-8"/>
</bean>

3、文件下载

private void downFile(String filename, String realPath, HttpServletResponse response) throws IOException {
try {
filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
log.warn("文件转码出现异常异常信息为:" + ExceptionUtil.print(e));
}
log.debug("下载的文件名:" + filename);
log.debug("下载的文件路径:" + realPath);
OutputStream os = null;
InputStream inputStream = null;
try {
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setContentType("application/x-download");
response.setHeader("Content-Disposition", "attachment;fileName="
+ filename);
inputStream = new FileInputStream(realPath);
os = response.getOutputStream();
byte[] b = new byte[1024];
int length;
while ((length = inputStream.read(b)) != -1) {
os.write(b, 0, length);
}
os.flush();
} catch (IOException e) {
log.warn("下载文件出现异常,异常信息为:" + ExceptionUtil.print(e));
} finally {
inputStream.close();
os.close();
}
}

参考:springmvc文件上传

数据导出:java 导出excel到客户端(本地)例子 用poi和servlet实现的

springmvc之图片上传的更多相关文章

  1. SSM(Spring+springMVC+MyBatis)框架-springMVC实现图片上传

    关于springMVC来实现图片上传的功能 话不多说,直接上码 1.applicationContext.xml <!-- 配置文件上传 --> <!--200*1024*1024即 ...

  2. springmvc的图片上传与导出显示

    1.前端文件上传需要在form表单内添加enctype="multipart/form-data" ,以二进制传递: 2.web.xml文件内配置 <servlet-mapp ...

  3. springmvc图片上传、json

    springmvc的图片上传 1.导入相应的pom依赖 <dependency> <groupId>commons-fileupload</groupId> < ...

  4. springmvc上传图片并显示图片--支持多图片上传

    实现上传图片功能在Springmvc中很好实现.现在我将会展现完整例子. 开始需要在pom.xml加入几个jar,分别是: <dependency> <groupId>comm ...

  5. Spring+SpringMVC+MyBatis+easyUI整合优化篇(七)图片上传功能

    日常啰嗦 前一篇文章<Spring+SpringMVC+MyBatis+easyUI整合优化篇(六)easyUI与富文本编辑器UEditor整合>讲了富文本编辑器UEditor的整合与使用 ...

  6. SpringMVC框架五:图片上传与JSON交互

    在正式图片上传之前,先处理一个细节问题: 每一次发布项目,Tomcat都会重新解压war包,之前上传过的图片会丢失 为了解决这个问题:可以不在Tomcat下保存图片,而是另找一个目录. 上传图片: & ...

  7. SpringMVC 图片上传,检查图片大小

    使用SpringMVC+Spring 前端提交图片文件到Controller,检查上传图片大小是否符合要求 直接上代码了 1.校验图片大小 这里提供出验证的方法,用于在需要校验的地方调用 /** * ...

  8. SpringMVC+Spring+MyBatis 整合与图片上传简单示例

    一.思路: (一) Dao层: 1. SqlMapConfig.xml,空文件即可.需要文件头.2. applicationContext_dao.xml. a) 数据库连接池b) SqlSessio ...

  9. jsp+springmvc实现文件上传、图片上传和及时预览图片

    1.多文件上传:http://blog.csdn.net/a1314517love/article/details/24183273 2.单文件上传的简单示例:http://blog.csdn.net ...

随机推荐

  1. linux下history命令显示历史指令记录的使用方法

    Linux系统当你在shell(控制台)中输入并执行命令时,shell会自动把你的命令记录到历史列表中,一般保存在用户目录下的.bash_history文件中.默认保存1000条,你也可以更改这个值 ...

  2. Appium for IOS testing on Mac

    一:环境 1.Mac OS X 10.9.1 2.Xcod 5.0.2 3.Appium 1.3.6 下载地址:https://bitbucket.org/appium/appium.app/down ...

  3. Android 使用 DownloadManager 管理系统下载任务的方法,android管理系统

    从Android 2.3(API level 9)开始Android用系统服务(Service)的方式提供了Download Manager来优化处理长时间的下载操作.Download Manager ...

  4. Linux Basic --- The First Character in The File Properity

    -rw-r--r-- [d]: content [-]: file [l]: link file [b]: interface device for storage in a device file ...

  5. git如何放弃所有本地修改?

    问题描述: 本地做了一些修改,我用git rebase说有冲突.我现在想把本地的请求都干掉,可能有的已经commit过了(没有push过),完全同步成远程版本,应该用什么命令? 使用命令: git r ...

  6. CSS------当内容超出div宽度后自动换行

    <div class="AllReceivers-normal" style="widht:100%;height:100%;word-wrap: break-wo ...

  7. Codeforces 719B Anatoly and Cockroaches(元素的交叉排列问题)

    题目链接:http://codeforces.com/problemset/problem/719/B 题目大意: 有一队蟑螂用字符串表示,有黑色 ‘b’ 和红色 'r' 两种颜色,你想使这队蟑螂颜色 ...

  8. 轻量级Image Library

    dlib http://sourceforge.net/projects/dclib/ stb https://github.com/nothings/stb

  9. 修改输入框placeholder文字默认颜色-webkit-input-placeholder

    html5为input添加了原生的占位符属性placeholder,高级浏览器都支持这个属性,例如: <input type="text" placeholder=" ...

  10. authorization配置

    在 Web.config 文件的<configuration>标记的子标记<authorization>和</authorization>之间用于设置应用程序的授权 ...