//注:上传文件的表单,需要将form标签设置enctype="multipart/form-data"属性,意思是将Content-Type设置成multipart/form-data
<form action="xxx" method="post" enctype="multipart/form-data">
<input type="text" name="name" id="id1" /><br />
<input type="password" name="password" /><br />
<input type="file" name="file" value="选择文件"/><input id="submit_form" type="submit" value="提交"/>
//1.form表单//注:上传文件的表单,需要将form标签设置enctype="multipart/form-data"属性,意思是将Content-Type设置成multipart/form-data
<form action="xxx" method="post" enctype="multipart/form-data">
<input type="text" name="name" id="id1" /><br />
<input type="password" name="password" /> <br />
<input type="file" name="file" value="选择文件"/><input id="submit_form" type="submit" value="提交"/>
</form>
//2.servlet实现文件接收的功能
boolean isMultipart = ServletFileUpload.isMultipartContent(request)//判断是否是表单文件类型
DiskFileItemFactory factory = new DiskFileItemFactory()
ServletFileUpload sfu = new ServletFileUpload(factory)
List items = sfu.parseRequest(request)//从request得到所有上传域的列表
for(Iterator iter = items.iterator()iter.hasNext()){
FileItem fileitem =(FileItem) iter.next() if(!fileitem.isFormField()&&fileitem!=null){
//判读不是普通表单域即是file
System.out.println("name:"+fileitem.getName())
}
}
3.扩展一下springboot
@RequestMapping("/xxx")
@ResponseBody
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
if (!file.isEmpty()) {
try {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(
file.getOriginalFilename())))
System.out.println(file.getName())
out.write(file.getBytes())
out.flush()
out.close()
} catch (FileNotFoundException e) {
e.printStackTrace()
return "上传失败," + e.getMessage()
} catch (IOException e) {
e.printStackTrace()
return "上传失败," + e.getMessage()
}
return "上传成功"
} else {
return "上传失败,因为文件是空的."
}
}
/**
* 文件上传处理主程序。
*
* @return int 操作结果 0 文件操作成功;1 request对象不存在。 2 没有设定文件保存路径或者文件保存路径不正确;3
* 没有设定正确的enctype;4 文件操作异常。
*/
public Map<String, String> fileupload_java(HttpServletRequest request,String uploadpath) {
Map<String, String> param = new HashMap<String, String>()
try {
// 参数或者文件名
String name = null
// 参数的value
String value = null
// 读取的流是否为文件的标志位
boolean fileFlag = false
// 要存储的文件。
File tmpFile = null
// 上传的文件的名字
String fName = null
FileOutputStream baos = null
BufferedOutputStream bos = null
int rtnPos = 0
byte[] buffs = new byte[BUFSIZE * 8]
// 取得ContentType
String contentType = request.getContentType()
int index = contentType.indexOf("boundary=")
String boundary = "--" + contentType.substring(index + 9)
String endBoundary = boundary + "--"
// 从request对象中取得流。
ServletInputStream sis = request.getInputStream()
// 读取1行
while ((rtnPos = sis.readLine(buffs, 0, buffs.length)) != -1) {
String strBuff = new String(buffs, 0, rtnPos)
if (strBuff.startsWith(boundary)) {
if (name != null && name.trim().length() > 0) {
if (fileFlag) {
bos.flush()
baos.close()
bos.close()
baos = null
bos = null
param.put(name, tmpFile.getAbsolutePath())
} else {
String paramValue = param.get(name)
paramValue += ","+ value
param.put(name, paramValue)
}
}
name = new String()
value = new String()
fileFlag = false
fName = new String()
rtnPos = sis.readLine(buffs, 0, buffs.length)
if (rtnPos != -1) {
strBuff = new String(buffs, 0, rtnPos)
if (strBuff.toLowerCase().startsWith("content-disposition: form-data ")) {
int nIndex = strBuff.toLowerCase().indexOf("name=\"")
int nLastIndex = strBuff.toLowerCase().indexOf("\"", nIndex + 6)
name = strBuff.substring(nIndex + 6, nLastIndex)
}
int fIndex = strBuff.toLowerCase().indexOf("filename=\"")
if (fIndex != -1) {
fileFlag = true
int fLastIndex = strBuff.toLowerCase().indexOf("\"", fIndex + 10)
// fName = strBuff.substring(fIndex + 10, fLastIndex)
fName = new String(strBuff.substring(fIndex + 10, fLastIndex).getBytes(),"gbk")
fName = FileL.getFileNameWithoutSeprater(fName)
if (fName == null || fName.trim().length() == 0) {
fileFlag = false
sis.readLine(buffs, 0, buffs.length)
sis.readLine(buffs, 0, buffs.length)
sis.readLine(buffs, 0, buffs.length)
continue
}else{
fName = FileL.getFileNameTime(fName)
sis.readLine(buffs, 0, buffs.length)
sis.readLine(buffs, 0, buffs.length)
}
}
}
} else if (strBuff.startsWith(endBoundary)) {
if (name != null && name.trim().length() > 0) {
if (fileFlag) {
bos.flush()
baos.close()
bos.close()
baos = null
bos = null
param.put(name, tmpFile.getAbsolutePath())
} else {
String paramValue = param.get(name)
paramValue += ","+ value
param.put(name, paramValue)
}
}
} else {
if (fileFlag) {
if (baos == null && bos == null) {
tmpFile = new File(uploadpath + fName)
baos = new FileOutputStream(tmpFile)
bos = new BufferedOutputStream(baos)
}
bos.write(buffs, 0, rtnPos)
baos.flush()
} else {
value = value + strBuff
}
}
}
} catch (IOException e) {
e.printStackTrace()
}
return param
}