SpringMVC上传图片与访问

网友投稿 173 2023-07-22


SpringMVC上传图片与访问

关于springmvc上传图片的方法给大家整理了两种方法,具体内容如下所示:

第一种:(放在该项目下的物理地址对应的位置)

a. 路径写法:

String basePath="/WEB-INF/resources/upload";

String filePathName= request.getSession().getServletContext().getRealPath(basePath);存放路径

b. 实际路径:

D:\WorkSpace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\XYT\WEB-INF\resources\upload\图片名称

c. 访问路径: http://localhost:8080/XYT/resources/upload/图片名称

d. 前提:只要这个项目能运行就行。

第二种:(创建虚拟路径,配置Tomcat下server.xml,创建存储路径和访问路径)

1.路径写法:

String filePathName=Constant.IMG_PATH+File.separator+"upload";

其中:public static final String IMG_PATH = "E:\\java\\img";

2.路径配置:

Server.xml配置

unpackWARs="true" autoDeploy="true">

prefix="localhost_access_log." suffix=".txt"

pattern="%h %l %u %t "%r" %s %b" />

unpackWARs="true" autoDeploy="true">

prefix="localhost_access_log." suffix=".txt"

pattern="%h %l %u %t "%r" %s %b" />

prefix="localhost_access_log." suffix=".txt"

pattern="%h %l %u %t "%r" %s %b" />

3.实际路径:E:\Java\img\upload

4.访问路径:http://localhost:8080/upload/图片名称

5.参考:http://my.oschina.net/pingdy/blog/381001

6.前提:必须打开Tomcat服务器

举例:上传图片的实例:(可以上传多张图片)

jsONObject rs=new JSONObject();

CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(

request.getSession().getServletContext());

String url="";

if (multipartResolver.isMultipart(request)) {

MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;

Iterator iter = multiRequest.getFileNames();

while (iter.hasNext()) {

MultipartFile file = multiRequest.getFile((String) iter.next());

if (file != null) {

String originalFileName = file.getOriginalFilename();

String[] f = originalFileName.split("\\.");

String ext = "";

if(f!=null && f.length>1){

ext = f[f.length-1];

System.out.println(ext);

}

System.out.println(GopyvallowImgType==null);

if(!allowImgType.contains(ext.toUpperCase())){

rs.put("code", "ERR_UPLOAD_0003");

rs.put("msg", "类型错误");

return rs;

}

//String basePath="/WEB-INF/resources/upload";//String filePathName = request.getSession().getServletContext().getRealPath(basePath);

String filePathName=Constant.IMG_PATH+File.separator+"upload";

url = filePathName;

System.out.println(url);

//上传后记录在path这个路径下。

File localFile = new File(filePathName);

if(!localFile.exists()){

localFile.mkdir();

}

//compress

String fname =new Date().getTime()+"."+ext;

String originalFname = fname.substring(0,fname.indexOf("."))+"_original."+ext;

String fileName = filePathName + File.separator + fname;

String oFileName = filePathName + File.separator + originalFname;

File infile = new File(fileName);

File oFile = new File(oFileName);

try{

ImageHelper.compress(file.getInputStream(), 600, infile);

file.transferTo(oFile);//original 上传原图

JSONObject obj = new JSONObject();

rs.put("code", Constant.CODE_SUCCESS);

rs.put("data", obj.toString());

}catch(Exchttp://eption e){

rs.put("code", "ERR_UPLOAD_0001");

rs.put("msg", "ERR_UPLOAD_0001");

e.printStackTrace();

return rs;

}

}

以上所述是针对SpringMVC上传图片与访问的相关内容,希望对大家有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:全方位讲解Java的面向对象编程思想
下一篇:Spring自定义配置Schema可扩展(二)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~