servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<context:component-scan base-package="com" />
<!--
<context:property-placeholder
location="/WEB-INF/spring/*.properties"
system-properties-mode="OVERRIDE" />
-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/longinghou/*" />
<bean class="com.sino.interceptor.longingInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
<mvc:annotation-driven />
<mvc:default-servlet-handler />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
<util:properties id="controller" location="/WEB-INF/spring/controller.properties" />
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 设置上传文件的最大尺寸为1MB -->
<property name="maxUploadSize">
<value>1048576</value>
</property>
</bean>
</beans>
HTML:
<%--
Document : longing
Created on : 2014-7-28, 14:53:08
Author : Administrator
--%>
<%@page import="java.util.Date"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%--<%@page contentType="text/html" pageEncoding="UTF-8"%>--%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/jq.min.js"></script>
<script type="text/javascript">
var chenggongyu="${chenggongyu}";
if(chenggongyu!=""){
alert("生成成功");
}
function addone() {
var ss = "<div>标题:<input type=‘text‘ name=‘title‘><br>描述:<input type=‘text‘ name=‘miaoshu‘><br>URL:<input type=‘text‘ name=‘url‘><br> image:<input type=‘file‘ name=‘urlName‘><br><br><br></div>";
// alert($("#xuanze").html())
// $("#xuanze").html+ss;
//$("#xuanze").html($("#xuanze").html() + ss);
$("#xuanze").append(ss);
// document.getElementById("xuanze").innerHTML+=ss;
}
function submitss() {
var titlearr = new Array();
var miaoshuarr = new Array();
var urlarr = new Array();
$("input[name=‘title‘]").each(
function() {
titlearr.push($(this).val());
}
);
$("input[name=‘miaoshu‘]").each(
function() {
miaoshuarr.push($(this).val());
}
);
$("input[name=‘url‘]").each(
function() {
urlarr.push($(this).val());
}
);
$("#titlearr").val(titlearr);
$("#miaoshuarr").val(miaoshuarr);
$("#urlarr").val(urlarr);
return true;
}
</script>
</head>
<body>
<form method="post" id="form1" action="<%=path%>/longinghou/succesfile" enctype="multipart/form-data" onsubmit="return submitss()">
欢迎您:${username}
<div id="xuanze">
第一行内容(头标题)<br>
标题:<input type="text" name="title"><br>
描述:<input type="text" name="miaoshu"><br>
URL:<input type="text" name="url"><br>
image:<input type="file" name="urlName"><br><br><br>
</div>
<input type="submit" value="提交" >        
<input type="button" value="再来一行" onclick="addone()">
<input type="hidden" id="titlearr" name="titlearr"/>
<input type="hidden" id="miaoshuarr" name="miaoshuarr"/>
<input type="hidden" id="urlarr" name="urlarr"/>
</form>
<!-- //action里的html是后缀名,不是HTML文件,用于spring对请求进行拦截判断
<form method="post" action="<%=path%>/longinghou/succesfile" enctype="multipart/form-data">
<input type="text" name="name" />
<input type="file" name="urlName" />
<input type="file" name="urlName" />
<input type="submit" />
</form>-->
</body>
</html>
Controller:
@RequestMapping(value = "/succesfile")
public String handleRequest(HttpServletRequest request,
HttpServletResponse response, @RequestParam MultipartFile[] urlName, @RequestParam String[] titlearr, @RequestParam String[] miaoshuarr, @RequestParam String[] urlarr) throws Exception {
// for (int i = 0; i < urlName.length; i++) {
// String fileName = urlName[i].getOriginalFilename();//获取上传第一个图片的名字
// System.out.println(urlName[i].getOriginalFilename());
// }
// for (int i = 0; i < titlearr.length; i++) {
// String fileName = titlearr[i];//获取上传第一个图片的名字
// System.out.println(titlearr[i]);
// }
// for (int i = 0; i < miaoshuarr.length; i++) {
// String fileName = miaoshuarr[i];//获取上传第一个图片的名字
// System.out.println(miaoshuarr[i]);
// }
// for (int i = 0; i < urlarr.length; i++) {
// String fileName = urlarr[i];//获取上传第一个图片的名字
// System.out.println(urlarr[i]);
// }
String neirong = "";
try {
String encoding = "GBK";
File file = new File(feile + "/temp.txt");
if (file.isFile() && file.exists()) { //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
neirong += lineTxt;
}
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
String lineirong = "";
for (int i = 0; i < urlName.length; i++) {
String neirongtihuan = tihuanneirong;
String title = titlearr[i];
String miaoshu = miaoshuarr[i];
String url = urlarr[i];
String img = urlName[i].getOriginalFilename();
if (i == 0) {
neirong = neirong.replace("{title1}", title);
neirong = neirong.replace("{miaoshu1}", miaoshu);
neirong = neirong.replace("{url1}", url);
neirong = neirong.replace("{img1}", img);
} else {
neirongtihuan = neirongtihuan.replace("{title}", title);
neirongtihuan = neirongtihuan.replace("{miaoshu}", miaoshu);
neirongtihuan = neirongtihuan.replace("{url}", url);
neirongtihuan = neirongtihuan.replace("{img}", img);
lineirong += neirongtihuan;
}
}
neirong = neirong.replace("{duogeli}", lineirong);
SimpleDateFormat df = new SimpleDateFormat("MMdd");//设置日期格式
String wenjianj = df.format(new Date());
File outfile = new File(feile + "/" + wenjianj);
if (!outfile.exists()) {
outfile.mkdirs();
} else {
fu.delFolder(feile + "/" + wenjianj);
outfile.mkdirs();
}
File htmlfile = new File(feile + "/" + wenjianj + "/hq.html");
htmlfile.createNewFile();
try {
FileOutputStream writerStream = new FileOutputStream(feile + "/" + wenjianj + "/hq.html");
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8")); //写入流
output.write(neirong); //s1为写入的字符串
output.flush();
output.close();
} catch (Exception e) {
log.info("html文件写入失败");
}
File imgfile = new File(feile + "/" + wenjianj + "/img");
imgfile.mkdirs();
//上传图片
for (int i = 0; i < urlName.length; i++) {
MultipartFile myfile = urlName[i];
if (myfile.isEmpty()) {
log.info("文件未上传");
} else {
String path = feile + "/" + wenjianj + "/img";
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
// 这里不必处理IO流关闭的问题,因为FileUtils.copyInputStreamToFile()方法内部会自动把用到的IO流关掉,我是看它的源码才知道的
// FileUtils.copyInputStreamToFile(myfile.getInputStream(),
// new File(path, myfile.getOriginalFilename()));
FileUtils.writeByteArrayToFile(new File(path, urlName[i].getOriginalFilename()),
myfile.getBytes());
}
}
request.setAttribute("chenggongyu", "1");
return "longinghou";
}
完整项目名称
neirongfabu
需要完整项目的请留言
Spring MVC多图片上传,多值上传,布布扣,bubuko.com
原文地址:http://blog.csdn.net/ufo2910628/article/details/38256831