使用commons-fileupload实现表单提交上传,并取出参数,解决了乱码
2009-09-29 22:21
543 查看
File tempfile = null;
List itemsList = null;
try {
tempfile = new File(System.getProperty("java.io.tmpdir"));
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setSizeThreshold(4096);
diskFileItemFactory.setRepository(tempfile);
ServletFileUpload sfu = new ServletFileUpload(diskFileItemFactory);
sfu.setHeaderEncoding("utf-8");
sfu.setSizeMax(4194304);
itemsList = sfu.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = itemsList.iterator();
String fileName = null;
while (itr.hasNext()) {
FileItem fi = (FileItem) itr.next();
/*********************************取出表单域中的参数以及对应提交的值***************/
if (fi.isFormField()) {
if (fi.getFieldName().equals("receiver")) {
//解决中文参数乱码问题
receiver = new String(fi.getString("UTF-8"));
}
if (fi.getFieldName().equals("sender")) {
//解决中文参数乱码问题
sender = new String(fi.getString("UTF-8"));
}
if (fi.getFieldName().equals("title")) {
//解决中文参数乱码问题
title = new String(fi.getString("UTF-8"));
//解决中文参数乱码问题
}
if (fi.getFieldName().equals("content")) {
//解决中文参数乱码问题
content = new String(fi.getString("UTF-8"));
}
} else {
/*******************************上传文件部分*****************************/
fileName = fi.getName();
if (fileName != null&&fileName.length()!=0) {
File fullFile = new File(fi.getName());
String fileType=fullFile.getName().substring(fullFile.getName().lastIndexOf("."));
//避免上传文件重名
String saveFileName=new SimpleDateFormat("yyMMddHHssmm").format(new Date())+String.valueOf((int)Math.random()*1000)+fileType;
//文件上传到的文件夹在web。xml中进行配置,通过servletcontext取得
String uploadPath=getServletContext().getInitParameter("uploadPath");
File filePath=new File(uploadPath);
if(!filePath.exists()){
filePath.mkdirs();
}
File savedFile = new File(
uploadPath, saveFileName);
try {
fi.write(savedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
List itemsList = null;
try {
tempfile = new File(System.getProperty("java.io.tmpdir"));
DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();
diskFileItemFactory.setSizeThreshold(4096);
diskFileItemFactory.setRepository(tempfile);
ServletFileUpload sfu = new ServletFileUpload(diskFileItemFactory);
sfu.setHeaderEncoding("utf-8");
sfu.setSizeMax(4194304);
itemsList = sfu.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = itemsList.iterator();
String fileName = null;
while (itr.hasNext()) {
FileItem fi = (FileItem) itr.next();
/*********************************取出表单域中的参数以及对应提交的值***************/
if (fi.isFormField()) {
if (fi.getFieldName().equals("receiver")) {
//解决中文参数乱码问题
receiver = new String(fi.getString("UTF-8"));
}
if (fi.getFieldName().equals("sender")) {
//解决中文参数乱码问题
sender = new String(fi.getString("UTF-8"));
}
if (fi.getFieldName().equals("title")) {
//解决中文参数乱码问题
title = new String(fi.getString("UTF-8"));
//解决中文参数乱码问题
}
if (fi.getFieldName().equals("content")) {
//解决中文参数乱码问题
content = new String(fi.getString("UTF-8"));
}
} else {
/*******************************上传文件部分*****************************/
fileName = fi.getName();
if (fileName != null&&fileName.length()!=0) {
File fullFile = new File(fi.getName());
String fileType=fullFile.getName().substring(fullFile.getName().lastIndexOf("."));
//避免上传文件重名
String saveFileName=new SimpleDateFormat("yyMMddHHssmm").format(new Date())+String.valueOf((int)Math.random()*1000)+fileType;
//文件上传到的文件夹在web。xml中进行配置,通过servletcontext取得
String uploadPath=getServletContext().getInitParameter("uploadPath");
File filePath=new File(uploadPath);
if(!filePath.exists()){
filePath.mkdirs();
}
File savedFile = new File(
uploadPath, saveFileName);
try {
fi.write(savedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
相关文章推荐
- 使用commons-fileupload实现文件(doc、pdf)上传,提取表单参数并解决中文乱码
- java web 利用 commons-fileupload-1.2 实现文件上传 解决 中文乱码& 非文件属性的读取
- 使用Springmvc和commons-fileupload-1.2.2.jar实现上传文件时,中文文件名乱码
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- android客户端通过Get方式提交参数给服务器,使用URL和HttpURLConnection实现,以及乱码问题解决
- java web 利用 commons-fileupload-1.2 实现文件上传 解决 中文乱码& 非文件属性的读取
- 使用commons-fileupload实现单个和多个文件上传
- ANDROID使用MULTIPARTENTITYBUILDER实现类似FORM表单提交方式的文件上传
- 解决表单提交参数乱码问题【终极版】不看后悔
- 关于C++使用libcurl提交表单方式上传文件时提交表单参数[同HTML表单]
- struts2表单提交的中文参数后台乱码问题解决
- 使用struts2提交jsp页面表单时参数中汉字乱码的问题
- 【java】[文件上传jar包]commons-fileUpload组件解决文件上传(文件名)乱码问题
- 表单添加图片,commons-fileupload实现上传
- Servlet使用commons-fileupload实现文件上传
- apache-commons-fileupload 提交表单中文乱码
- 使用commons-fileupload实现文件上传
- 使用commons-fileupload实现上传
- 使用Apache的commons-fileupload和commmons-io组件实现文件上传
- 使用apache的commons-fileupload实现多文件上传