fileinput组件将文件上传到服务器的路径编写方法以及Tomcat的配置

1:先配置文件上传时Tomcat的虚拟路径

system.file.read.location=http://localhost:8080/fileserversavepath/ 这是访问图片的地址
system.file.write.location=D\:/static_files/fileserversavepath/ 这是Tomcat下临时存储文件的虚拟路径

2:接着要找到存储图片的服务器,找到server.xml配置映射路径(红色区域就是添加的配置)

<Host name="localhost"  appBase="webapps"
            unpackWARs="true" autoDeploy="true">


        <!-- SingleSignOn valve, share authentication between web applications
             Documentation at: /docs/config/valve.html -->
        <!--
        <Valve className="org.apache.catalina.authenticator.SingleSignOn" />
        -->


        <!-- Access log processes all example.
             Documentation at: /docs/config/valve.html
             Note: The pattern used is equivalent to using pattern="common" -->
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="localhost_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />


<Context path="/fileserversavepath" docBase="D:/static_files/fileserversavepath/" reloadable="true" crossContext="true" />
      </Host>

3:图片上传到上面建立的虚拟路径下,通过以下代码实现,注意文件名称保存到数据库的方式,要相对路径进行保存,然后将得到的相对路径通过json进行返回到页面,通过提交的方式保存到数据库中,下面是实现代码

writeLocation = PropertiesUtil.getPropertiesValue("system.file.write.location", Constants.APP_PROPERTIES_PATH);
MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request; 
MultipartFile  mfile =  mRequest.getFile("locationtype");
fileFileName = mfile.getOriginalFilename();//获取文件名
fileSize = mfile.getSize();

try {

//request.getSession().getServletContext().getRealPath("upload/img/product");
String pathString = writeLocation;
String fileurl = "upload/";
String saveurl = "";
String filepathString = pathString + fileurl;这是要保存到本地磁盘的虚拟路径

if(fileFileName != null){
// 构建文件对象

File folder = new File(filepathString);
// 检测文件夹是否存在,如果不存在,则新建images目录
if (!folder.exists()) {
folder.mkdir();
}
String[] str = fileFileName.split("\\.");
int index = str.length;
String suffix = str[index - 1];// fileFileName[i].split("\\.")[1];
String fileName = getImgName() + "." + suffix;
  mfile.transferTo(new File(filepathString + "\\" + fileName));
saveurl  += id+":"+fileurl + fileName + ":" + java.net.URLEncoder.encode(fileFileName,"UTF-8") + ":" + fileSize + ":" + suffix;这是要保存到数据库里的图片名称,注意,后面的字段是防止名称重复,进行保存的时候要进行字符串的截取
//outputStream.close();

Map<String, Object> map = new HashMap<String, Object>();
map.put("data", "success");
map.put("msg", saveurl);
writeJSON(response, map);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值