Java圖片上傳/檔案上傳__Java

來源:互聯網
上載者:User

圖片上傳/檔案上傳 


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);

}
}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.