commons-io下载地址:http://commonsapacheorg/io/download_iocgi
common-fileupload组件是apache的一个开源项目之一,可以从http://jakartaapacheorg/commons/fileupload/下载。
该组件简单易用,可实现一次上传一个或多个文件,并可限制文件大小。
下载后解压zip包,将commons-fileuploadjar,和commons-io里面后缀为jar复制到你的项目的webapp\WEB-INF\lib\下,如果目录不存在请自建目录。
这个项目是用来上传文件,文件路径为workspace\项目名称\build\weboutput\file\项目下,如果没有该文件夹请创建一个。否则会发生找不到路径的情况
import javaioFile;
import javaioIOException;
import javaioPrintWriter;
import javautilIterator;
import javaxservletServletException;
import javaxservlethttpHttpServlet;
import javaxservlethttpHttpServletRequest;
import javaxservlethttpHttpServletResponse;
import orgapachecommonsfileuploadFileItem;
import orgapachecommonsfileuploadFileItemFactory;
import orgapachecommonsfileuploaddiskDiskFileItemFactory;
import orgapachecommonsfileuploadservletServletFileUpload;
/
Servlet implementation class FileUpload
/
public class FileUpload extends HttpServlet {
private static final long serialVersionUID = 1L;
/
@see HttpServlet#HttpServlet()
/
public FileUpload() {
super();
// TODO Auto-generated constructor stub
}
/
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//判断提交过来的表单是否为文件上传菜单
boolean isMultipart= ServletFileUploadisMultipartContent(request);
if(isMultipart){
//构造一个文件上传处理对象
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
Iterator items;
try{
//解析表单中提交的所有文件内容
items=uploadparseRequest(request)iterator();
while(itemshasNext()){
FileItem item = (FileItem) itemsnext();
if(!itemisFormField()){
//取出上传文件的文件名称
String name = itemgetName();
//取得上传文件以后的存储路径
String fileName=namesubstring(namelastIndexOf('\\')+1,namelength());
//上传文件以后的存储路径
String path= requestgetRealPath("file")+FileseparatorChar+fileName;
//上传文件
File uploaderFile = new File(path);
itemwrite(uploaderFile);
//打印上传成功信息
responsesetContentType("text/html");
responsesetCharacterEncoding("GB2312");
PrintWriter out = responsegetWriter();
outprint("<font size='2'>上传文件为:"+name+"<br>保存的地址为"+path+ "</font>");
}
}
}catch(Exception e){
eprintStackTrace();
}
}
}
}
http://blog163com/lin305_gf/blog/static/969524402011718102116625/
这是给你转载的网易博客的
servlet上传文件
如果你是用的 框架 比如struts2 那就更简单一点了
以上就是关于java获得上传文件的路径全部的内容,包括:java获得上传文件的路径、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)