我写了个上传文件的程序,代码如下:
在红色的地方,出现问题,没有得到值,为空。麻烦各位帮我看看是那出了问题,我实在是找不出问题了谢谢指教
private String uploadPath = "D:\\temp"; // 上传文件的目录
private String tempPath = "D:\\"; // 临时文件目录
File tempPathFile;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 System.out.println(items.size()+"fff");
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}<%@ page language="java" pageEncoding="GBK"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
</head>
<body>
<form name="myform" action="Upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"/>
<input type="submit" value="导入" />
</form>
</body>
</html>
在红色的地方,出现问题,没有得到值,为空。麻烦各位帮我看看是那出了问题,我实在是找不出问题了谢谢指教
private String uploadPath = "D:\\temp"; // 上传文件的目录
private String tempPath = "D:\\"; // 临时文件目录
File tempPathFile;
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
try {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(4096); // 设置缓冲区大小,这里是4kb
factory.setRepository(tempPathFile);// 设置缓冲区目录
ServletFileUpload upload = new ServletFileUpload(factory);
upload.setSizeMax(4194304); // 设置最大文件尺寸,这里是4MB
List<FileItem> items = upload.parseRequest(request);// 得到所有的文件 System.out.println(items.size()+"fff");
Iterator<FileItem> i = items.iterator();
while (i.hasNext()) {
FileItem fi = (FileItem) i.next();
String fileName = fi.getName();
if (fileName != null) {
File fullFile = new File(fi.getName());
File savedFile = new File(uploadPath, fullFile.getName());
fi.write(savedFile);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}<%@ page language="java" pageEncoding="GBK"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.util.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
</head>
<body>
<form name="myform" action="Upload" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"/>
<input type="submit" value="导入" />
</form>
</body>
</html>
解决方案 »
- 怎么把电脑上登录的用户信息存在cookie里面,在下次打开的时候方便检索历史登录过的用户名
- iterator标记怎样比较前后值.
- 如何在 web.xml 文件中处理 500和404错误?
- 有没有免费的既能生成统计图(如:饼图、柱状图)同时又能生成统计数据表格列表的java包?
- 令人困惑的 PHP/Java Bridge + php5-cgi
- 怎么让系统每天自动计费,紧急~
- 如何在web 页面上实现图片放大镜功能?
- 解答这一个地方,其他的两个地方都给加分,有人能回答吗?300分了!
- 很简单的一个问题,在线等候回音、、、、、
- 求助 struts2 拦截器跳转登录页面问题
- 请教:模拟单点登录
- url传参数的问题
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<jsp:useBean id="smartupload" class="org.lxh.smart.SmartUpload"/>
<html>
<head>
<title>smartupload</title>
</head>
<body>
<%
//String name= request.getParameter("username");
//因为form为数据封装了所以,request是不能接到参数的
request.setCharacterEncoding("GBK");
smartupload.initialize(pageContext) ; // 初始化上传
smartupload.upload() ; // 准备上传
String name =smartupload.getRequest().getParameter("username");
String fileName =this.getServletContext().getRealPath("/")+"/upload"+name;
smartupload.getFiles().getFile(0).saveAs(fileName); // 保存文件
%>
<h1><%=name%></h1>
<img src="upload/<%=name%>" width="300" height="200">
</body>
</html>
org.apache.commons.fileupload这个做的啊