Java图片上传/文件上传

1 篇文章 0 订阅
1 篇文章 0 订阅

图片上传/文件上传 


1. 创建一个web项目 新建一个JSP

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/upload.action" method="post" enctype="multipart/form-data">
请选择上传的文件/图片:
<input type="file" name="file"/>
<!-- 请选择上传的图片: -->
<!-- <input type="file" name="file" accept="image/*" /> -->
<input type="submit" value="上传"/>
</form>
</body>
</html>


2 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <display-name></display-name>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
  <servlet-name>SpringMVC</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:SpringMVC.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
  <servlet-name>SpringMVC</servlet-name>
  <url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <filter>
  <filter-name>CharacterEncodingFilter</filter-name>
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  <init-param>
  <param-name>encoding</param-name>
  <param-value>UTF-8</param-value>
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>CharacterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
</web-app>

3 applicationContext.xml 


4 SpringMVC.xml

<beans>

<!-- 很重要 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"></property>
</bean>

</beans>


5 Controller:

package com.bwie.controller;


import java.io.File;
import java.io.IOException;
import java.util.UUID;


import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;



@Controller
public class PowerController {

@RequestMapping("upload")
public void upload(HttpServletRequest request){
//获取支持文件上传的Request对象 MultipartHttpServletRequest
MultipartHttpServletRequest mtpreq = (MultipartHttpServletRequest) request;
//通过 mtpreq 获取文件域中的文件
MultipartFile file = mtpreq.getFile("file");
//通过MultipartFile 对象获取文件的原文件名 
String fileName = file.getOriginalFilename();
//生成一个uuid 的文件名
UUID randomUUID = UUID.randomUUID();
//获取文件的后缀名
int i = fileName.lastIndexOf(".");
String uuidName = randomUUID.toString()+fileName.substring(i);
//获取服务器的路径地址(被上传文件的保存地址)
String realPath = request.getSession().getServletContext().getRealPath("/file");
//将路径转化为文件夹 并 判断文件夹是否存在
File dir = new File(realPath);
if(!dir.exists()){
dir.mkdir();
}
//获取一个文件的保存路径
String path = realPath+"/"+uuidName;


// 为文件这服务器中开辟一给新的空间,*没有数据
// File newFile = new File(path); 

try {
file.transferTo(new File(path));
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

System.err.println("-----服务器的路径地址为::"+realPath);
System.err.println("-----图片名称为::"+fileName);
System.err.println("-----图片新名称为::"+uuidName);
System.err.println("-----图片新路径为::"+path);

}
}




  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值