springMVC 文件上传 优化代码记录。

本文介绍了一个使用Spring MVC实现文件上传的示例,包括了前端页面的编写和后端控制器的处理逻辑,展示了如何配置Spring MVC以支持文件上传功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Controller<span style="white-space:pre">	</span>

package com.hmx.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;


@Controller
//根目录
@RequestMapping("/file")
public class FileLoad {
	
	//优化版上传文件!
	@RequestMapping("/upload2")
	//RequestParam(jsp里的 name)   页面将所有信息都传送request表单,
	public String upload2(HttpServletRequest request,HttpServletResponse response) throws IllegalStateException, IOException{
		//定义一个解析器 将springMVC上下文解析到
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
		//用解析器解析request里面的数据,并判断是否为Multipart类型的数据
		if(multipartResolver.isMultipart(request)){
			//request如果是Multipart类型的数据,将reques转化成MultipartHttpServletRequest类型 
			MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)request;
			//拿文件  通过迭代器Iterator一个一个拿文件
			Iterator<String> iter = multiRequest.getFileNames();
			
			while(iter.hasNext()){
				//根据迭代器拿到的文件名称,传给file
				MultipartFile file = multiRequest.getFile((String)iter.next());
				//开始写文件
				if( file!=null){
					//上传后的文件名称	
					String fileName="上传后的文件  :"+file.getOriginalFilename();
					//上传后的文件位置
					String path="D:/"+fileName;
					
					File localFile = new File(path);
					//将上传文件(file)写到指点文件上(localFile)
					file.transferTo(localFile);
				}
			}
		}
		return "/success";
}
	
	
	@RequestMapping("/upload")
					//RequestParam(jsp里的 name)
	public String addFile(@RequestParam("file")CommonsMultipartFile file, HttpServletRequest request) throws IOException{
		System.out.println("文件上传      :"+file.getOriginalFilename());
		//上传文件,判断是否有文件
		if(!file.isEmpty()){
			try {
				//输出流        文件上传后保存的地方
				FileOutputStream os=new FileOutputStream("D:/"+file.getOriginalFilename());
				//输入流
				InputStream in=file.getInputStream();
				//读文件(一个一个字节读)
				int b=0;
				while((b=in.read())!=-1){
					os.write(b);
				}
				//关闭流
				os.flush();
				os.close();
				in.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
		return "/success";
	}
	

	
	@RequestMapping("/toload")
	public String toLoad(){
		
		return "upload";
	}
	
}
springMVC-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans	
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-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
	http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
	
	<!-- 启动SpringMVC的注解功能-->
	<mvc:annotation-driven/>
	<!-- 加载包中的controller包(controller类所在的包名)    加载扫描注解包 -->
	<context:component-scan base-package="com.hmx.controller"/>
	
	<!-- 静态资源访问  (不拦截images文件夹下的文件)-->
	 <!-- 不拦截静态资源 <mvc:default-servlet-handler />  -->  					
	   <mvc:resources location="/images/" mapping="/images/**"/>
	   <mvc:resources location="/js/" mapping="/js/**"/>
	
	<!-- 多请求处理控制器 -->   
	   <bean id="paramMethodResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">     
	<!-- 自己定义的配置标识action,当访问页地址栏输入访问名加上"?action=方法名"-->  
	   <property name="paramName" value="action">    
	   </property>     
	</bean> 
	
	<!-- 视图解析器  -->    
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">             
		<property name="prefix" value="/"></property>      
		<property name="suffix" value=".jsp"></property>  
	</bean>
	
	<!-- 文件上传 -->
	<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="defaultEncoding" value="utf-8"></property>
		<property name="maxUploadSize" value="10485760000"></property>
		<property name="maxInMemorySize" value="40960"></property>
	</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>springMVC4</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.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*:/config/springMVC-servlet.xml</param-value>  
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

uoload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form name="userForm" action="/springMVC4/file/upload2" method="post" enctype="multipart/form-data">
		选择文件:<input type="file" name="file">
		<div><input type="submit" value="提交">
		</div>
	</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	上传文件成功!
</body>
</html>


内容概要:本文详细介绍了扫描单分子定位显微镜(scanSMLM)技术及其在三维超分辨体积成像中的应用。scanSMLM通过电调透镜(ETL)实现快速轴向扫描,结合4f检测系统将不同焦平面的荧光信号聚焦到固定成像面,从而实现快速、大视场的三维超分辨成像。文章不仅涵盖了系统硬件的设计与实现,还提供了详细的软件代码实现,包括ETL控制、3D样本模拟、体积扫描、单分子定位、3D重建和分子聚类分析等功能。此外,文章还比较了循环扫描与常规扫描模式,展示了前者在光漂白效应上的优势,并通过荧光珠校准、肌动蛋白丝、线粒体网络和流感A病毒血凝素(HA)蛋白聚类的三维成像实验,验证了系统的性能和应用潜力。最后,文章深入探讨了HA蛋白聚类与病毒感染的关系,模拟了24小时内HA聚类的动态变化,提供了从分子到细胞尺度的多尺度分析能力。 适合人群:具备生物学、物理学或工程学背景,对超分辨显微成像技术感兴趣的科研人员,尤其是从事细胞生物学、病毒学或光学成像研究的科学家和技术人员。 使用场景及目标:①理解和掌握scanSMLM技术的工作原理及其在三维超分辨成像中的应用;②学习如何通过Python代码实现完整的scanSMLM系统,包括硬件控制、图像采集、3D重建和数据分析;③应用于单分子水平研究细胞内结构和动态过程,如病毒入侵机制、蛋白质聚类等。 其他说明:本文提供的代码不仅实现了scanSMLM系统的完整工作流程,还涵盖了多种超分辨成像技术的模拟和比较,如STED、GSDIM等。此外,文章还强调了系统在硬件改动小、成像速度快等方面的优势,为研究人员提供了从理论到实践的全面指导。
内容概要:本文详细介绍了基于Seggiani提出的渣层计算模型,针对Prenflo气流床气化炉中炉渣的积累和流动进行了模拟。模型不仅集成了三维代码以提供气化炉内部的温度和浓度分布,还探讨了操作条件变化对炉渣行为的影响。文章通过Python代码实现了模型的核心功能,包括炉渣粘度模型、流动速率计算、厚度更新、与三维模型的集成以及可视化展示。此外,还扩展了模型以考虑炉渣组成对特性的影响,并引入了Bingham流体模型,更精确地描述了含未溶解颗粒的熔渣流动。最后,通过实例展示了氧气-蒸汽流量增加2%时的动态响应,分析了温度、流动特性和渣层分布的变化。 适合人群:从事煤气化技术研究的专业人士、化工过程模拟工程师、以及对工业气化炉操作优化感兴趣的科研人员。 使用场景及目标:①评估不同操作条件下气化炉内炉渣的行为变化;②预测并优化气化炉的操作参数(如温度、氧煤比等),以防止炉渣堵塞;③为工业气化炉的设计和操作提供理论支持和技术指导。 其他说明:该模型的实现基于理论公式和经验数据,为确保模型准确性,实际应用中需要根据具体气化炉的数据进行参数校准。模型还考虑了多个物理场的耦合,包括质量、动量和能量守恒方程,能够模拟不同操作条件下的渣层演变。此外,提供了稳态求解器和动态模拟工具,可用于扰动测试和工业应用案例分析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值