Springmvc Mode upload File

Source: Internet
Author: User

Here are three ways to upload are used in the project, are similar to the main or use SPRINGMVC inside the multipartfile file

Package com.xiaoquan.approval.controller.admin;

Import Java.io.File;
Import Java.text.DateFormat;
Import Java.text.SimpleDateFormat;
Import Java.util.Arrays;
Import Java.util.Date;
Import Java.util.UUID;

Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

Import Org.apache.commons.io.FileUtils;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.ui.Model;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RequestParam;
Import Org.springframework.web.bind.annotation.ResponseBody;
Import Org.springframework.web.multipart.MultipartFile;

Import Com.xiaoquan.approval.core.BaseController;
Import Com.xiaoquan.approval.domain.T_ProjectFile;
Import Com.xiaoquan.approval.domain.T_ProjectIdea;
Import Com.xiaoquan.approval.domain.T_Users;
Import Com.xiaoquan.approval.modules.admin.service.T_ProjectFileService;
Import Com.xiaoquan.approval.modules.admin.service.T_ProjectIdeaService;
Import Com.xiaoquan.approval.modules.admin.service.T_UsersService;
Import Com.xiaoquan.approval.utils.EncryptAlgorithm;
Import Com.xiaoquan.approval.utils.SecurityContextUtil;
@Controller
@RequestMapping (value = "/admin/file/*")
public class Adminfileuploadcontroller extends Basecontroller {
@Autowired private T_projectfileservice Filesservice;
@Autowired private T_usersservice T_usersservice;
@Autowired private T_projectideaservice T_projectideaservice;
Project files
@ResponseBody
@RequestMapping (value = "/upload/adjuncted", produces = "text/html;charset=utf-8", Method=requestmethod.post)
Public String Adjunct (httpservletrequest request,httpservletresponse response,
@RequestParam ("Files") multipartfile[] Files,integer type,string filename0,long projectid) {
if (files==null| | Files.length!=1) {
Return Getajax (1, "Please select a file", null);
}
Multipartfile Myfile=files[0];
if (Myfile.isempty ()) {
Return Getajax (1, "Please select a file", null);
}
T_projectfile tf=new t_projectfile ();
String Realpath=null;
String Htmlpath=null;
String Tmppath =this.getclass (). GetResource (""). GetPath ();
String storagepath= tmppath.substring (0, Tmppath.lastindexof ("/web"));
try {
DateFormat dateformat=new SimpleDateFormat ("/yyyy/mm/dd/");
String prefix= "/adjunct";
if (type==0) {
prefix= "/adjunct";
}else{
prefix= "/files";
}
String upload= "/upload";
String Datapath=dateformat.format (New Date ());
File Filedir=new file (Storagepath+upload+prefix+datapath);
if (!filedir.exists ()) {
Filedir.mkdirs ();
}
String fileName = Myfile.getoriginalfilename ();

Extended Name
String Fileext = filename.substring (Filename.lastindexof (".") + 1). toLowerCase ();
/*if (!fileext.equals ("Zip")) {
Return getfailure ("File Upload as Compressed package! ");
}*/

if (! Arrays.<string>aslist ("zip,rar,gz,bz2". Split (",")). Contains (Fileext)) {
Return Getajax (300, "The upload file extension is not allowed. \ n only allow "+" zip,rar,gz,bz2 "+" format ", null);
}
Uuid
String Uuidname=uuid.randomuuid (). toString (). Replace ("-", "" ");
Realpath=storagepath+upload+prefix+datapath+uuidname+ "." +fileext;
Htmlpath=upload+prefix+datapath+uuidname+ "." +fileext;
File File=new file (Realpath);
Fileutils.copyinputstreamtofile (Myfile.getinputstream (), file);

Tf.setprojectfilepath (Htmlpath);
Rename, change name
if (null! = FILENAME0 &&! "". Equals (FILENAME0)) {
Tf.setremark (FILENAME0);
}else{
Return Getfailure ("The name cannot be empty!") ");
}


Filesservice.saveorupdate (TF);

} catch (Exception e) {
E.printstacktrace ();
Return getfailure ("system error");
}
Return Getsuccee ("saved successfully! ", TF);
}


Avatar
@ResponseBody
@RequestMapping (value = "/upload/edit", produces = "text/html;charset=utf-8")
Public String picture (Model model,integer type,string newsrc) {
T_users user = Securitycontextutil.getsessionuser ();
T_users tf=new t_users ();
if (newsrc==null| | (""). Equals (NEWSRC)) {
Return Getajax (1, "Please select a file", null);
}
String Htmlpath=null;
String upload= "/upload";
String prefix= "/touxiang";
String Tmppath =this.getclass (). GetResource (""). GetPath ();
String storagepath= tmppath.substring (0, Tmppath.lastindexof ("/web"));
try {
DateFormat dateformat=new SimpleDateFormat ("/yyyy/mm/dd/");
String Datapath=dateformat.format (New Date ());

Newsrc=newsrc.substring (22);
File File=new file (Storagepath+upload+prefix+datapath+user.getid () + ". png");
Byte[] A=encryptalgorithm.decodebase64 (NEWSRC);
Fileutils.writebytearraytofile (file, a);

Htmlpath=upload+prefix+datapath+user.getid () + ". png";
Tf.setid (User.getid ());
Tf.setpath (Htmlpath);

T_usersservice.saveorupdate (TF);

} catch (Exception e) {
E.printstacktrace ();
Return getfailure ("system error");
}
Return Getsuccee ("saved successfully! ", TF);
}


@ResponseBody
@RequestMapping (value = "/upload/partitionidea", produces = "text/html;charset=utf-8", Method=requestmethod.post)
Public String Partitionidea (httpservletrequest request,httpservletresponse response,
@RequestParam ("Files") multipartfile[] Files,integer type,string FILENAME0) {
if (files==null| | Files.length!=1) {
Return Getajax (1, "Please select a file", null);
}
Multipartfile Myfile=files[0];
if (Myfile.isempty ()) {
Return Getajax (1, "Please select a file", null);
}
T_projectidea tf=new T_projectidea ();
String Realpath=null;
String Htmlpath=null;
String Tmppath =this.getclass (). GetResource (""). GetPath ();
String storagepath= tmppath.substring (0, Tmppath.lastindexof ("/web"));
try {
DateFormat dateformat=new SimpleDateFormat ("/yyyy/mm/dd/");
String prefix= "/adjunct";
if (type==0) {
prefix= "/adjunct";
}else{
prefix= "/files";
}
String upload= "/upload";
String Datapath=dateformat.format (New Date ());
File Filedir=new file (Storagepath+upload+prefix+datapath);
if (!filedir.exists ()) {
Filedir.mkdirs ();
}
String fileName = Myfile.getoriginalfilename ();

Extended Name
String Fileext = filename.substring (Filename.lastindexof (".") + 1). toLowerCase ();
Uuid
String Uuidname=uuid.randomuuid (). toString (). Replace ("-", "" ");
Realpath=storagepath+upload+prefix+datapath+uuidname+ "." +fileext;
Htmlpath=upload+prefix+datapath+uuidname+ "." +fileext;
File File=new file (Realpath);
Fileutils.copyinputstreamtofile (Myfile.getinputstream (), file);
Rename, change name
if (null! = FILENAME0 &&! "". Equals (FILENAME0)) {
Tf.setpicturename (FILENAME0);
}else{
Return Getfailure ("The name cannot be empty!") ");
}
Tf.setpicture (Htmlpath);

} catch (Exception e) {
E.printstacktrace ();
Return getfailure ("system error");
}
Return Getsuccee ("saved successfully! ", TF);
}


}

Springmvc Mode upload File

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.