Spring中servletFileUpload完成上传文件以及文本的处理
JSP:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="test.do" method="post" enctype="multipart/form-data">
名字:<input name="username" type="text"><br />
<input name="imgFile" id="imgFile" type="file" /><br />
<input type="submit" value="提交">
</form>
</body>
</html>
JAVA:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class tt
{
@RequestMapping(value = "test")
public void test(HttpServletRequest request)
{
boolean flag = ServletFileUpload.isMultipartContent(request); if (flag)
{
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try
{
// 解析request请求
List<FileItem> list = upload.parseRequest(request);
for (FileItem item : list)
{
System.out.println(item); String fileName = item.getFieldName();
// 判断是否是普通的输入项
if (item.isFormField())
{
// 解析文本
if (fileName.equals("username"))
{
try
{
String username = item.getString("UTF-8");
System.out.println(username);
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
}
}
// 上传图片
if (!item.isFormField())
{
InputStream in = null;
FileOutputStream out = null;
try
{
in = item.getInputStream();
out = new FileOutputStream(new File("E:/test.txt")); byte[] buff = new byte[1024];
int len = 0;
while ((len = in.read(buff)) != -1)
{
out.write(buff, 0, len);
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (in != null)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (out != null)
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
} }
}
}
}
catch (FileUploadException e)
{
e.printStackTrace();
} } }
}
在Spring中不用配置:否则会导致request请求两次解析造成解析为空值的问题(详情可以参考:http://blog.csdn.net/lwphk/article/details/43015829)
<!-- 支持上传文件 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value ="1024" />
<property name="resolveLazily" value="true"/>
<property name="defaultEncoding" value = "UTF-8" />
Spring中servletFileUpload完成上传文件以及文本的处理的更多相关文章
- Spring Boot 在接收上传文件时,文件过大异常处理问题
Spring Boot 在接收上传文件时,文件过大时,或者请求过大,spring内部处理都会抛出异常,并且捕获不到. 虽然可以通过调节配置,增大 请求的限制值. 但是还是不太方便. 之所以捕获不到异常 ...
- asp.net中FileUpload得到上传文件的完整路径
asp.net中FileUpload得到上传文件的完整路径 Response.Write("完整路径:" + Server.MapPath(FileUpload1.PostedFi ...
- Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件)
Java中使用HttpPost上传文件以及HttpGet进行API请求(包含HttpPost上传文件) 一.HttpPost上传文件 public static String getSuffix(fi ...
- 【Spring Boot】关于上传文件例子的剖析
目录 Spring Boot 上传文件 功能实现 增加ControllerFileUploadController 增加ServiceStorageService 增加一个Thymeleaf页面 修改 ...
- SSM框架中如何简便上传文件表单
此种方式上传文件相对简单,以下均经测试成功,才提供到此. 以下为单个文件上传方式 分析:本次的工作目的是根据一级标题产生对应的二级标题,在每个二级标题下对应一个(file字段)新闻文件,当点击新闻文件 ...
- javaWeb中使用ajax上传文件
javaWeb上传图片 上传文件所必要的两个jar包:commons-fileupload.jar.commons-io.jar. jar包下载:github路径 核心代码: String withP ...
- Linux中ftp不能上传文件/目录的解决办法
在linux中不能上传文件或文件夹最多的问题就是权限问题,但有时也不一定是权限问题了,像我就是空间不够用了,下面我来总结一些ftp不能上传文件/目录的解决办法 在排除用户组和权限等问题后,最可能引 ...
- 利用ServletFileUpload组件上传文件
自己的运用: public void UploadNoteFile(HttpServletRequest request,HttpServletResponse response){ String ...
- 使用PuTTY在Windows中向Linux上传文件
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3843207.html ...
随机推荐
- MATLAB绘图
matlab绘制散点图 clc,clear x=[11.9,11.5,14.5,15.2,15.9,16.3,14.6,12.9,15.8,14.1]; y=[196.84,196.84,197.14 ...
- mysql count(*)和count(列)速率
count(*)通常是对主键进行索引扫描,count(列)不一定 count(*)是统计表中所有符合的记录总数,count(列)是计算表中所有符合的列的记录数 count的时候,如果没有where限制 ...
- Hadoop组件构成
Hadoop平台重要组件: 1.ZooKeeper 一个分布式应用程序协调服务. 包含简单的原语集.实现统一命名服务.配置管理.分布式锁服务.集群管理等功能. 2.Cascading 架构在 Hado ...
- C语言回顾-整型变量修饰符和一维数组
1.整型变量修饰符 1)改变整型变量的存储空间 #include <stdio.h> int main(int argc, const char * argv[]) { //改变整型变量占 ...
- 很久以前写的一个 ShareRestrictedSD 类
代码中一开始的 几个 USES 单元,可能是多余的. unit ShareRestrictedSD; interface uses Windows, Messages, SysUtils, Class ...
- mysql索引的使用和优化
参考: http://blog.csdn.net/xluren/article/details/32746183 http://www.cnblogs.com/hustcat/archive/2009 ...
- Spring Batch学习笔记三:JobRepository
此系列博客皆为学习Spring Batch时的一些笔记: Spring Batch Job在运行时有很多元数据,这些元数据一般会被保存在内存或者数据库中,由于Spring Batch在默认配置是使用H ...
- java中常见的几种异常
算术异常类:ArithmeticExecption空指针异常类:NullPointerException类型强制转换异常:ClassCastException数组负下标异常:NegativeArray ...
- Java中ProcessBuilder应用实例
系列说明 浅析Java.lang.Runtime类 浅析Java.lang.Process类 浅析Java.lang.ProcessBuilder类 可以使用java中的ProcessBuilder执 ...
- Curling 2.0
Description On Planet MM-21, after their Olympic games this year, curling is getting popular. But th ...