文件上传服务器方式,普通方式表单文件上传服务器

web.xml

/p>

"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

"http://java.sun.com/dtd/web-app_2_3.dtd" >

Archetype Created Web Application

dispatcher

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:springmvc.xml

1

dispatcher

/

index.jsp

Hello World! mvc

springmvc.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

controller

package org.lee;

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;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import java.io.File;

import java.util.List;

@Controller

public class MyFile {

@RequestMapping("/file")

public String file(HttpServletRequest request) throws Exception {

String realPath = request.getSession().getServletContext().getRealPath("/upload");

File file = new File(realPath);

if (!file.exists()){

file.mkdir();

}

DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory();

ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);

List fileItems = servletFileUpload.parseRequest(request);

for (FileItem fileItem : fileItems) {

if (fileItem.isFormField()){

}else {

System.out.println(fileItem.getName());

fileItem.write(new File(realPath,fileItem.getName()));

}

}

System.out.println(realPath);

return "hello";

}

}

mvn坐标

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.lee

mvc

1.0-SNAPSHOT

war

mvc Maven Webapp

http://www.example.com

UTF-8

1.7

1.7

4.3.16.RELEASE

junit

junit

4.11

test

org.springframework

spring-context

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

commons-fileupload

commons-fileupload

1.4

commons-io

commons-io

2.7

javax.servlet

javax.servlet-api

3.1.0

mvc

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值