「java表单文件上传」JAVA表单文件上传为什么不按顺序排列
本篇文章给大家谈谈java表单文件上传,以及JAVA表单文件上传为什么不按顺序排列对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java servlet 表单文件上传用什么类型接
- 2、java表单提交里面的文件上传,用同步的方式好还是用异步的好啊?
- 3、java fileItem如何遍历上传普通表单域值到数据库?
- 4、Java中上传文件和表单数据提交如何质莸
java servlet 表单文件上传用什么类型接
/**
* 文件上传处理主程序。
*
* @return int 操作结果 0 文件操作成功;1 request对象不存在。 2 没有设定文件保存路径或者文件保存路径不正确;3
* 没有设定正确的enctype;4 文件操作异常。
*/
public MapString, String fileupload_java(HttpServletRequest request,String uploadpath) {
MapString, String param = new HashMapString, 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;
}
java表单提交里面的文件上传,用同步的方式好还是用异步的好啊?
你好,很高兴回答你的问题。
这里做成异步比较好。如果同步的话,遇到文件大的话,表单提交会很慢,体验非常不好。
如果有帮助到你,请点击采纳。
java fileItem如何遍历上传普通表单域值到数据库?
//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="提交"/
Java中上传文件和表单数据提交如何质莸
//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 "上传失败,因为文件是空的.";
}
}
java表单文件上传的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于JAVA表单文件上传为什么不按顺序排列、java表单文件上传的信息别忘了在本站进行查找喔。
发布于:2022-11-29,除非注明,否则均为
原创文章,转载请注明出处。