java 获取表单file 内容然后传到java类中可在java类中显示 帮帮忙 写详细一点啊 新手求助 10
1个回答
展开全部
package com.servlet;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.PropertyResourceBundle;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import com.dao.impl.AddReportDaoImpl;
import com.dao.impl.ReadProperties;
//添加模板
public class ReportFileUploadServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private ServletConfig config;
File tmpDir = null;// 初始化上传文件的临时存放目录
File saveDir = null;// 初始化上传文件后的保存目录
// 调用报表名(供导出时执行)
private String reportTName = null;
// 报表名称(用户自写)
private String reportName = null;
// 报表描述(用户自写)
private String reportInfo = null;
// 报表导出保存路径
private String savePath = null;
// 模板ID
private String rid = "";
//报表路径
private String reportPath = "";
private AddReportDaoImpl addReportDaoImpl = new AddReportDaoImpl();
public ReportFileUploadServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
try {
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory dff = new DiskFileItemFactory();// 创建该对象
dff.setRepository(tmpDir);// 指定上传文件的临时目录
dff.setSizeThreshold(1024000);// 指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);// 创建该对象
sfu.setFileSizeMax(5000000);// 指定单个上传文件的最大尺寸
sfu.setSizeMax(10000000);// 指定一次上传多个文件的 总尺寸
FileItemIterator fii = sfu.getItemIterator(request);// 解析request
// 请求,并返回FileItemIterator集合
while (fii.hasNext()) {
FileItemStream fis = fii.next();// 从集合中获得一个文件流
String name = new String(fis.getFieldName().getBytes(
"ISO-8859-1"), "utf-8");
InputStream stream = fis.openStream();
if (!fis.isFormField() && fis.getName().length() > 0) {// 过滤掉表单中非文件域
String fileName = fis.getName();// 获得上传文件的文件名
BufferedInputStream in = new BufferedInputStream(fis
.openStream());// 获得文件输入流
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(saveDir + "\\"
+ fileName)));// 获得文件输出流
Streams.copy(in, out, true);// 开始把文件写到你指定的上传文件夹
} else {
if (name.equals("reportName")) {
reportTName = Streams.asString(stream, "utf-8");
}
if (name.equals("reportInfo")) {
reportInfo = Streams.asString(stream, "utf-8");
}
if (name.equals("path")) {
savePath = Streams.asString(stream, "utf-8");
}
if (name.equals("rid")) {
rid = Streams.asString(stream, "utf-8");
}
if(name.equals("reportPath")){
reportPath = Streams.asString(stream, "utf-8");
}
}
}
String showInfo = "";
String callInfo = addReportDaoImpl.addReport(reportTName,
reportName, reportInfo, savePath, rid,reportPath);
if (callInfo.equals("true")) {
showInfo = "alert('操作成功!');";
} else if (callInfo.equals("repeat")) {
showInfo = "alert('" + reportTName + "模板已添加,不允许重复添加!');";
} else if (callInfo.equals("flase")) {
showInfo = "alert('添加失败,请确定模板信息是否正确!');";
}
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out
.print("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
out.print("<script>");
out.print(showInfo);
out.print("window.top.location.href='/ReportTimer';");
out.print("</script>");
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out
.print("<meta http-equiv='Content-Type' content='text/html; charset=GBK'>");
out.print("<script>");
out.print("alert('操作失败!');");
out.print("window.top.location.href='/ReportTimer';");
out.print("</script>");
out.flush();
out.close();
}
}
public void init(ServletConfig sc) throws ServletException {
String projectPath = "";
ReadProperties rp = new ReadProperties();
projectPath = rp.reportPath();
/*
* 对上传文件夹和临时文件夹进行初始化
*
*/
// // 得到项目路径
// String releasePath = ParameterTransition.class.getResource("/")
// .getPath();
// // 得到webapps所在索引位置
// int index = releasePath.indexOf("webapps");
// // 截取项目路径
// String CutOut = releasePath.substring(1, index);
// // 拼接项目路径得到所需项目路径
// projectPath = CutOut
// + "webapps\\WebReport\\WEB-INF\\reportlets\\";
super.init();
config = sc;
String tmpPath = "d:\\tmpdir";
String savePath = projectPath;
tmpDir = new File(tmpPath);
saveDir = new File(savePath);
if (!tmpDir.isDirectory())
tmpDir.mkdir();
if (!saveDir.isDirectory())
saveDir.mkdir();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.PropertyResourceBundle;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import com.dao.impl.AddReportDaoImpl;
import com.dao.impl.ReadProperties;
//添加模板
public class ReportFileUploadServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private ServletConfig config;
File tmpDir = null;// 初始化上传文件的临时存放目录
File saveDir = null;// 初始化上传文件后的保存目录
// 调用报表名(供导出时执行)
private String reportTName = null;
// 报表名称(用户自写)
private String reportName = null;
// 报表描述(用户自写)
private String reportInfo = null;
// 报表导出保存路径
private String savePath = null;
// 模板ID
private String rid = "";
//报表路径
private String reportPath = "";
private AddReportDaoImpl addReportDaoImpl = new AddReportDaoImpl();
public ReportFileUploadServlet() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
try {
if (ServletFileUpload.isMultipartContent(request)) {
DiskFileItemFactory dff = new DiskFileItemFactory();// 创建该对象
dff.setRepository(tmpDir);// 指定上传文件的临时目录
dff.setSizeThreshold(1024000);// 指定在内存中缓存数据大小,单位为byte
ServletFileUpload sfu = new ServletFileUpload(dff);// 创建该对象
sfu.setFileSizeMax(5000000);// 指定单个上传文件的最大尺寸
sfu.setSizeMax(10000000);// 指定一次上传多个文件的 总尺寸
FileItemIterator fii = sfu.getItemIterator(request);// 解析request
// 请求,并返回FileItemIterator集合
while (fii.hasNext()) {
FileItemStream fis = fii.next();// 从集合中获得一个文件流
String name = new String(fis.getFieldName().getBytes(
"ISO-8859-1"), "utf-8");
InputStream stream = fis.openStream();
if (!fis.isFormField() && fis.getName().length() > 0) {// 过滤掉表单中非文件域
String fileName = fis.getName();// 获得上传文件的文件名
BufferedInputStream in = new BufferedInputStream(fis
.openStream());// 获得文件输入流
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(saveDir + "\\"
+ fileName)));// 获得文件输出流
Streams.copy(in, out, true);// 开始把文件写到你指定的上传文件夹
} else {
if (name.equals("reportName")) {
reportTName = Streams.asString(stream, "utf-8");
}
if (name.equals("reportInfo")) {
reportInfo = Streams.asString(stream, "utf-8");
}
if (name.equals("path")) {
savePath = Streams.asString(stream, "utf-8");
}
if (name.equals("rid")) {
rid = Streams.asString(stream, "utf-8");
}
if(name.equals("reportPath")){
reportPath = Streams.asString(stream, "utf-8");
}
}
}
String showInfo = "";
String callInfo = addReportDaoImpl.addReport(reportTName,
reportName, reportInfo, savePath, rid,reportPath);
if (callInfo.equals("true")) {
showInfo = "alert('操作成功!');";
} else if (callInfo.equals("repeat")) {
showInfo = "alert('" + reportTName + "模板已添加,不允许重复添加!');";
} else if (callInfo.equals("flase")) {
showInfo = "alert('添加失败,请确定模板信息是否正确!');";
}
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out
.print("<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
out.print("<script>");
out.print(showInfo);
out.print("window.top.location.href='/ReportTimer';");
out.print("</script>");
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out
.print("<meta http-equiv='Content-Type' content='text/html; charset=GBK'>");
out.print("<script>");
out.print("alert('操作失败!');");
out.print("window.top.location.href='/ReportTimer';");
out.print("</script>");
out.flush();
out.close();
}
}
public void init(ServletConfig sc) throws ServletException {
String projectPath = "";
ReadProperties rp = new ReadProperties();
projectPath = rp.reportPath();
/*
* 对上传文件夹和临时文件夹进行初始化
*
*/
// // 得到项目路径
// String releasePath = ParameterTransition.class.getResource("/")
// .getPath();
// // 得到webapps所在索引位置
// int index = releasePath.indexOf("webapps");
// // 截取项目路径
// String CutOut = releasePath.substring(1, index);
// // 拼接项目路径得到所需项目路径
// projectPath = CutOut
// + "webapps\\WebReport\\WEB-INF\\reportlets\\";
super.init();
config = sc;
String tmpPath = "d:\\tmpdir";
String savePath = projectPath;
tmpDir = new File(tmpPath);
saveDir = new File(savePath);
if (!tmpDir.isDirectory())
tmpDir.mkdir();
if (!saveDir.isDirectory())
saveDir.mkdir();
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询