java表单+多文件上传~~源代码

private static final String C_ACTION_CREAT_ACTIVITY = "meeting_register_addActivity";

@RequestMapping(value = "/addActivity")
public CIPResponseMsg addActivity(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("接口进来了");
CIPResponseMsg msg = new CIPResponseMsg();
CIPRuntimeOperator operator = CIPRuntime.getOperateSubject();
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
request.getParameterMap();
String name = request.getParameter("activity_name");
MEETING_register_activeityData activeityData = new MEETING_register_activeityData();
MEETING_register_activeityVO activeityVO = new MEETING_register_activeityVO();
// 文件存放路径
String path = CIPRuntimeConfigure.cip_temp_file_path + File.separator.replaceAll("\\\\", "/");
String tempPath;
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String fileName = "";
System.out.println(11);
try {
Iterator<FileItem> iter = FileUploadUtil.getFileItemIterator(
request, path);
while (iter.hasNext()) {
// item---前端传过来的(实体+表单)的所有数据
FileItem item = iter.next();
String fieldName = item.getFieldName();
Long fileSize = item.getSize();
fileName = item.getName();
// 判断属性是不是普通的表单类型
// 将前端数据一一对应塞进对象里面
if (!item.isFormField() && !"".equals(fileName)) {
// 截取前端传进来的文件后缀名
String suffix = fileName.substring(fileName
.lastIndexOf("."));
// 判断该附件格式是否符合条件
System.out.println("图片类型" + fileName);
if (".png".equalsIgnoreCase(suffix)) {
if ("background_picture".equals(fieldName)) {
String serverpath = path + "/" + fileName;
File file = new File(serverpath);
activeityVO.setBackground_picture(serverpath);
}
if ("seat_picture".equals(fieldName)) {
String serverpath = path + "/" + fileName;
File file = new File(serverpath);
activeityVO.setSeat_picture(serverpath);
}
if ("metting_agenda".equals(fieldName)) {
String serverpath = path + "/" + fileName;
File file = new File(serverpath);
activeityVO.setMetting_agenda(serverpath);
}
if ("metting_discipline".equals(fieldName)) {
String serverpath = path + "/" + fileName;
File file = new File(serverpath);
activeityVO.setMetting_discipline(serverpath);
}

} else {
msg.errorCode = 100005;
msg.msg = "附件格式输入有错,请重新输入!(仅支持png)";
return msg;
}
if (fileSize > 10485760) {
msg.errorCode = 100006;
msg.msg = fieldName + "上传图片不得大于10M!";
responseMsg(response, msg);
return msg;
}
} else {
// 表单内除附件外其他表的字段获取(通过映射set获取页面实体值id--name)
ReflectUtil.invokeSetMethod4Field(activeityData, fieldName,
new String(item.get(), "UTF-8"));
}

// 给图片重新命名+日期(用流重新写进来)
// fileName=TimeUtil.format(new Date(),
// "yyyyMMddHHmmss")+"_"+fileName;
fileName = fileName;
if (fileName == null || fileName.equals("")) {
// 文件夹名称为空,不写入
System.out.println("不写入");
} else {
tempPath = path.substring(0, path.length() - 1) + "/"
+ fileName;
System.out.println(tempPath);
File file = new File(tempPath);
file.createNewFile();
/*
* file.setExecutable(true);//设置可执行权限
* file.setReadable(true);//设置可读权限
* file.setWritable(true);//设置可写权限
*/// 写
item.write(file);
}
}

Integer activity_id = WCM_utils_SequenceUtils.nextItemNo();
activeityVO.setActivity_name(activeityData.getActivity_name());
activeityVO.setActivity_validate_mode(activeityData
.getActivity_validate_mode());
activeityVO.setActivity_start_time(activeityData
.getActivity_start_time());
activeityVO.setActivity_end_time(activeityData
.getActivity_end_time());
activeityVO
.setActivity_content(activeityData.getActivity_content());
String activity_name = activeityVO.getActivity_name();
// 活动名称不一致
List<MEETING_register_activeityData> ls = activeityService
.findName(activity_name);
if (ls.size() > 0) {
// 该任务名称数据库有,请重新命名
msg.errorCode = 1;
msg.msg = "活动名称已存在!请重新重命名";
} else {
String activity_start_time = activeityVO
.getActivity_start_time();
String activity_end_time = activeityVO.getActivity_end_time();
String activity_validate_mode = activeityVO
.getActivity_validate_mode();
String background_picture = activeityVO.getBackground_picture();
if (background_picture == null || background_picture.equals("")) {
background_picture = null;
} else {
String Path = background_picture;
background_picture = getPinYin.getPY(Path);
}
String seat_picture = activeityVO.getSeat_picture();
if (seat_picture == null || seat_picture.equals("")) {
seat_picture = null;
} else {
String newSeat_picture = seat_picture;
seat_picture = getPinYin.getPY(newSeat_picture);
}
String metting_agenda = activeityVO.getMetting_agenda();
if (metting_agenda == null || metting_agenda.equals("")) {
metting_agenda = null;
} else {
String agenda_newPath = metting_agenda;
metting_agenda = getPinYin.getPY(agenda_newPath);
}
String metting_discipline = activeityVO.getMetting_discipline();
if (metting_discipline == null || metting_discipline.equals("")) {
metting_discipline = null;
} else {
String discipline_newPath =metting_discipline;
metting_discipline = getPinYin.getPY(discipline_newPath);
}
String activity_content = activeityVO.getActivity_content();
//String text = "http://域名/项目名/页面.html?参数id="+id;
//二维码uat访问地址
String text = "http:/域名/项目名/页面?参数id="+id;
int width = 350; // 二维码图片的宽
int height = 350; // 二维码图片的高
String act_name = activity_name;
String py = getPinYin.getPY(act_name);                //此方法是将中文变成拼音。该源码贴在下面
String outPutPath = path + py + ".png"; //上传成功
outPutPath = outPutPath.replaceAll("\\\\", "/");
String imageType = "png"; // 二维码生成类型
QRCodeTool.zxingCodeCreate(text, width, height, outPutPath,imageType);
if (outPutPath.equals("") || outPutPath == null) {
msg.errorCode = 1;
msg.msg = "二维码生成失败";
} else {
activeityVO.setTwo_dimension_code(outPutPath);
//String two_code = activeityVO.getTwo_dimension_code();
String two_code = "";
String activity_status = "";
activeityService.addActivity(activity_id, activity_name,
activity_content, activity_start_time,
activity_end_time, activity_validate_mode,
background_picture, seat_picture, metting_agenda,
metting_discipline, two_code,
activity_status);
msg.errorCode = 0;
msg.msg = CIPErrorCode.CALL_SUCCESS.name;
}
}
} catch (CIPServiceException e) {
CIPErrorCode error = e.getErrorCode();
msg.errorCode = error.code;
msg.msg = error.name;
} catch (CIPDaoException e) {
CIPErrorCode error = e.getErrorCode();
msg.errorCode = error.code;
msg.msg = error.name;
} catch (CIPRuntimeException e) {
CIPErrorCode error = e.getErrorCode();
msg.errorCode = error.code;
msg.msg = error.name;
}
return msg;
}

//以下是中文转拼音方法

package com.yd.meeting.register.util;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

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.apache.log4j.Logger;

import com.yd.common.tool.dbm.StringUtil;
import com.yd.meeting.dongpu.controller.MEETING_dongpu_relationshipController;

public class getPinYin {

private final static Logger log = Logger.getLogger(MEETING_dongpu_relationshipController.class);
/**
* 根据汉字得到对应的拼音
*
* @param source
* @return
*/
public static String getPY(String source) {
if (StringUtil.isBlank(source)) {
return "";
}
char[] t1 = source.toCharArray();
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
format.setVCharType(HanyuPinyinVCharType.WITH_V);
StringBuilder result = new StringBuilder();
for (char aT1 : t1) {
if (Character.toString(aT1).matches("[\\u4E00-\\u9FA5]")) {
try {
result.append(PinyinHelper.toHanyuPinyinStringArray(aT1, format)[0]);
} catch (BadHanyuPinyinOutputFormatCombination e) {
log.error("转换汉字拼音错误!", e);
}
} else {
// 非汉字不进行转换,直接添加
result.append(aT1);
}
}
return result.toString();
}
}

原文地址:https://www.cnblogs.com/Darkqueen/p/10449748.html

时间: 2024-10-10 08:20:12

java表单+多文件上传~~源代码的相关文章

Struts2文件上传(基于表单的文件上传)

•Commons-FileUpload组件 –Commons是Apache开放源代码组织的一个Java子项目,其中的FileUpload是用来处理HTTP文件上传的子项目 •Commons-FileUpload组件特点 –使用简单:可以方便地嵌入到JSP文件中,编写少量代码即可完成文件的上传功能 –能够全程控制上传内容 –能够对上传文件的大小.类型进行控制 •需要下载Common-FileUplaod框架地址(当然MyEclipce中Struts2支持里自带有这两个包): –http://jak

AJAX提交form表单带文件上传

过了三天才想要写博客,这样不好,要改正 在做毕设的时候,用户发帖涉及到了文件上传的问题,在这里记录一下 背景: 在用户发帖的时候,用户只想发表文字postText,还有些用户想在发表postText的同时还发表一些图片,如何做? 上代码 不写的太细了,和流水账似的,挑重点记录一下. 1.前台的文件上传 本来想用form表单直接上传了,但是form提交时会刷新整个页面,但这不是我想要的,所以使用了ajax提交form表单. 利用ajax提交表单需要用到jquery.form.js这个包,网上有很多

云控后台系统 经验总结(表单和文件上传等)

1. qq emoji替换 接收到这样的字符串,通过正则匹配到每组方括号然后进行替换 var _str = i.content.replace(/\[.{1,3}\]/g, (str) => { //i.content为遍历出来的消息记录 var index; Object.keys(faceCode).forEach((key, _index) => { //faceCode为字符串与样式对应的关系,也可以通过参数去和后台约定好,不用这样查找 if (key === str) { index

(25) java web的struts2框架的使用-基于表单的文件上传

一,首先创建一个表单页面 <body> <form action="uploads" method="post" enctype="multipart/form-data"> 文件: <input type="file" name="uploadObject"> <input type="submit" value="提交"&

9.16 基于form表单的文件上传实现 ContextType

基于form表单传递普通键值对的方式传递数据: enctype="application/x-ww-form-urlencoded" 基于form表单上传文件 传递数据,非键值对: enctype="multipart/form-data" enctype="text/plain" 这种方式几乎不用 基于ajax传递普通键值对的方式传递数据到服务器 只要请求体有数据,request.body都有值 .但request.post 只有当conten

django_5:表单1——文件上传

上传文件1 class UserForm(forms.Form): name = forms.CharField() headImg = forms.FileField() def regist(req): if req.method == 'POST': uf = UserForm(req.POST, req.FILES) if uf.is_valid(): print(uf.cleaned_data['name']) #通过clean_data获取字段 print(req.FILES) #类

Java 利用 SWFUpload多文件上传 session 为空失效,不能验证的问题

原文转自:http://www.tqcto.com/article/code/295299.html 我们都知道普通的文件上传是通过表单进行文件上传的,还不能达到异步上传的目的.通过使用某些技术手段,比如jquery form.js可以达到异步上传的目的,但最重要的问题在于,它不能够进行多个文件的上传.如果你要上传多个文件,必须一个一个地上传,同时还要在界面上处理当上传完一个文件之后,下一个文件上传框的问题.    现在我们有了一个更多的运行,即使用swfupload进行多文件异步上传.顾名思义

form表单无刷新上传文件

很多时候,我们上传完文件之后,不想当前页面跳转,或者是刷新一下.那么我们需要怎么做呢? 首先,我们用的是最简单的form表单上传,提交方式.代码如下 <!--大家注意到这个form的target的了么?这个target属性的值frameFile,是form之后的 iframe的name值,这样的写法是让当前的form表单在提交表单内容的时候转交给iframe中进行页面 中表单处理,并且不会产生当前页面跳转!--> <form id="uploadForm" class

$_FILE参数详解及简单&lt;form&gt;表单无刷新上传文件

$_FILES:经由 HTTP POST 文件上传而提交至脚本的变量,类似于旧数组$HTTP_POST_FILES 数组(依然有效,但反对使用)详细信息可参阅 POST方法上传 $_FILES数组内容如下: $_FILES['myFile']['name'] 客户端文件的原名称 $_FILES['myFile']['type']   文件的 MIME类型,需要浏览器提供该信息的支持,例如"image/gif" $_FILES['myFile']['size']    已上传文件的大小,