关于commons-fileupload上传文件问题

gdhqs 2008-12-04 10:19:06
package com.he.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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;

public class UploadServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=gb2312");
PrintWriter out = response.getWriter();
System.out.println("上传文件");

// 设置保存上传文件的目录
String uploadDir = getServletContext().getRealPath("/") + "upload"; // 获得服务器的绝对路径
if(uploadDir == null) {
out.print("无法访问存储目录!");
return;
}
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists()) {
if(!fUploadDir.mkdir()) {
out.println("无法创建存储目录!");
return;
}
}

//设置存储临时文件的目录
String tempPath = "d:\\upload\\";
if(tempPath == null) {
out.print("无法访问临时存储目录!");
return;
}
File fTempPath = new File(tempPath);
if(!fTempPath.exists()) {
if(!fTempPath.mkdir()) {
return;
}
System.out.println("创建临时存储目录");
}

// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if(!isMultipart)
{
out.println("只能处理multipart/form-data类型的数据!");
return;
}

DiskFileItemFactory factory = new DiskFileItemFactory();

// 设置最多只允许在内存中存储的数据
factory.setSizeThreshold(1024*4);
//设置临时文件存储目录
factory.setRepository(new File(tempPath));

// 创建一个上传文件的ServletFileUpload对象
ServletFileUpload upload = new ServletFileUpload(factory);

// 设置最大的上传限制
upload.setSizeMax(1024*1024*50);


try{
// 处理HTTP请求,items是所有的表单项
List items = upload.parseRequest(request);
// 遍历所有的表单项
for(Iterator it = items.iterator(); it.hasNext();) {
FileItem item = (FileItem)it.next();
if(item.isFormField()) {
String name = item.getFieldName();
String value = item.getString("GBK");
System.out.println("表单域的name-value为:" + name +"=" + value);
}
else {
// 取得文件域的表单域名
String fieldName = item.getFieldName();
// 取得文件名
String fileName = item.getName();
// 取得文件类型
String contentType = item.getContentType();
// 以当前时间来生成上传文件的文件名
FileOutputStream fos = new FileOutputStream(request.getRealPath("/") +
System.currentTimeMillis() +
fileName.substring(fileName.lastIndexOf("."),fileName.length()));
// 如果上传文件域对应文件内容已经在内存中
if(item.isInMemory()) {
fos.write(item.get());
}
// 如果文件内容不完全在内存中
else {
// 获取上传文件内容的输入流
InputStream is = item.getInputStream();
byte[] buffer = new byte[1024];
int len;
// 读取上传文件的内容,并将其写入服务器的文件中
while((len = is.read(buffer)) > 0) {
fos.write(buffer,0,len);
}
is.close();
fos.close();
}
}
}
}catch(FileUploadException e) {
e.printStackTrace();
}
response.sendRedirect("MyJsp.jsp");
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}

}

请问,在哪里设置上传文件的保存目录?即uploadDir 要放在哪里?
...全文
458 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
forerver121 2008-12-06
  • 打赏
  • 举报
回复
如果存入你程序所在目录中:String uploadpath=request.getRealPath("/")+"你想存的文件夹名称"
如果存在指定的地方String uploadpath=“E:\\”;

FileOutputStream fos = new FileOutputStream(uploadpath +
System.currentTimeMillis() +
fileName.substring(fileName.lastIndexOf("."),fileName.length()));
fos.write(*,0,*);//*号你自己改吧
tiyuzhongxin789 2008-12-06
  • 打赏
  • 举报
回复
如果存入你程序所在目录中:String uploadpath=request.getRealPath("/")+"你想存的文件夹名称"
如果存在指定的地方String uploadpath=“E:\\”;

FileOutputStream fos = new FileOutputStream(uploadpath +
System.currentTimeMillis() +
fileName.substring(fileName.lastIndexOf("."),fileName.length()));
fos.write(*,0,*);//*号你自己改吧
zidasine 2008-12-06
  • 打赏
  • 举报
回复
FileOutputStream fos = new FileOutputStream(request.getRealPath("/") +
System.currentTimeMillis() +
fileName.substring(fileName.lastIndexOf("."),fileName.length()));
前面的路径用uploadDir 你创建保存文件夹的路径

FileOutputStream fos = new FileOutputStream(uploadDir+File.separator +
System.currentTimeMillis() +
fileName.substring(fileName.lastIndexOf("."),fileName.length()));
gdhqs 2008-12-06
  • 打赏
  • 举报
回复
楼上的貌似没说到重点
xiaohu0901 2008-12-04
  • 打赏
  • 举报
回复
FileOutPutStream实现文件字符流的输出!
gdhqs 2008-12-04
  • 打赏
  • 举报
回复
是创建了,但是要怎么写才能把上传的文件写入我创建的目录里呢?
zidasine 2008-12-04
  • 打赏
  • 举报
回复
// 设置保存上传文件的目录
String uploadDir = getServletContext().getRealPath("/") + "upload"; // 获得服务器的绝对路径
if(uploadDir == null) {
out.print("无法访问存储目录!");
return;
}
File fUploadDir = new File(uploadDir);
if(!fUploadDir.exists()) {
if(!fUploadDir.mkdir()) {
out.println("无法创建存储目录!");
return;
}
}
这段已经为你创建了这个目录 不需要手动去创建

67,543

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧