spring MVC 的MultipartFile转File读取

spring MVC 的MultipartFile转File读取,第1张

第一种方法

MultipartFile file = xxx

CommonsMultipartFile cf= (CommonsMultipartFile)file

DiskFileItem fi = (DiskFileItem)cf.getFileItem()

        File f = fi.getStoreLocation()

会在项目的根目录的临时文件夹下生成一个文件;

第二种方法:

 transferTo(File dest);

会在项目中生成一个新坦裤山文件;

第三种方法:   

File f = (File) xxx 强转即可。前提是要配置multipartResolver,要不然会报类型让中转换失败的异常。

第四种纯掘方法:

Workbook wb = Workbook.getWorkbook(xxx .getInputStream())

转换为输入流,直接读取;

第五种方法:

byte[] buffer = myfile.getBytes()

(1)在spring-mvc.xml加入基判:

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

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

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

<!-- 方式2:加载配置文件 -->

<util:properties id="applicationProps" location="classpath:conf/config_development.properties"/>

(2)在controller中加入:

@Autowired

private Properties applicationProps

public Properties getApplicationProps() {

return applicationProps

}

public void setApplicationProps(Properties applicationProps) {

this.applicationProps = applicationProps

}

(搏运改3).在controller中利用applicationProps读悄棚取配置文件

String jumpUrl = applicationProps.getProperty("learn.jump.url")

望采纳~

在使用hibernate或者spring的时候,我们往往通过配置文件配置数薯团据库连接属性。但这次咐颤项目中并没有用到hibernate和spring,只用到了struts2。要如何实现通过读取文件配置获取属性值呢?

方式一:ResourceBundle这个数简橘类可是实现读取properties文件来获取值

在java中:

public class ResourceBundleReader {

public final static Object initLock = new Object()

private final static String PROPERTIES_FILE_NAME = "property"

private static ResourceBundle bundle = null

static {

try {

if (bundle == null) {

synchronized (initLock) {

if (bundle == null)

bundle = ResourceBundle.getBundle(PROPERTIES_FILE_NAME,Locale.CHINA)

}

}

} catch (Exception e) {

System.out.println("读取资源文件property_zh.properties失败!")

}

}

public static ResourceBundle getBundle() {

return bundle

}

public static void setBundle(ResourceBundle bundle) {

bundle = bundle

}

}

在.properties文件中:

driverName=com.mysql.jdbc.Driver

url=xxxxx/:3307/9zgame?

user=root

password=xxxxxx

文件名字为:property_zh.properties。后zh根据Locale.CHINA一致的,如果Locale.ENGLISH,则文件名为:property_en.properties

方式二:使用commons组件。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/12239965.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-22
下一篇 2023-05-22

发表评论

登录后才能评论

评论列表(0条)

保存