SpringMVC upload pictures with access

  • 2020-05-05 11:11:40
  • OfStack

There are two ways to upload pictures from springmvc. The details are as follows:

first :(where the physical address under the item corresponds to)

a.

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

b. Actual path:

D: \ WorkSpace \. metadata \. plugins \ org eclipse. wst. server. core \ tmp0 \ wtpwebapps \ XYT \ WEB
- INF \ resources \ upload \ image name

c. Access path: http: / / localhost: 8080 / XYT/resources/upload
/picture name

d. Premise: as long as the project works.

second :(create virtual path, configure server.xml under Tomcat, create storage path and access path)

1. Path:

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

Where: public static final IMG_PATH = "E:\\Java\\img";

2. Path configuration:

Server.xml is configured with


<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
<!-- add(save pictures) -->
<Context path="/upload" docBase="E:\Java\img\upload"></Context>
</Host>

3. Actual path: E:\Java\img\upload

4. Access path: http: / / localhost: 8080 / upload
/picture name

5. Reference: http: / / my. oschina. net pingdy/blog
/ 381001

6. Prerequisite: Tomcat server
must be opened

Example: an example of uploading pictures: (multiple pictures can be uploaded)


JSONObject rs=new JSONObject();
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(
request.getSession().getServletContext());
String url="";
if (multipartResolver.isMultipart(request)) {
MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
Iterator<String> 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(allowImgType==null);
if(!allowImgType.contains(ext.toUpperCase())){
rs.put("code", "ERR_UPLOAD_0003");
rs.put("msg", " Type error ");
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);
// Record after uploading path Under this 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  Upload the artwork 
JSONObject obj = new JSONObject();
rs.put("code", Constant.CODE_SUCCESS);
rs.put("data", obj.toString());
}catch(Exception e){
rs.put("code", "ERR_UPLOAD_0001");
rs.put("msg", "ERR_UPLOAD_0001");
e.printStackTrace();
return rs;
}
}

The above is for SpringMVC upload pictures and access related content, I hope to help you.


Related articles: