Em relação ao método de fazer upload de imagens no SpringMVC, o editor compilou dois métodos para você, o conteúdo específico é o seguinte:
O primeiro tipo: (colocado no local correspondente do endereço físico no projeto)
um. Método de escrita de caminho:
String basalepath = "/web-inf/resources/upload";
String filepathname = request.getSession (). GetServletContext (). GetRealPath (baseepath);
b. Caminho real:
D: /workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/xyt/web-inf/resources/upload/image
c. Caminho de acesso: http: // localhost: 8080/xyt/recursos/upload/nome da imagem
d. Pré -requisito: desde que este projeto possa ser executado.
O segundo tipo: (Crie um caminho virtual, configure o Server.xml em Tomcat, crie caminho de armazenamento e caminho de acesso)
1. Escrita do caminho:
String filepathname = constant.img_path+file.separator+"upload";
Onde: public static final string img_path = "e: // java // img";
2. Configuração do caminho:
Configuração do server.xml
<Nome do host = "localhost" appbase = "webApps" unpackwars = "true" AutodelaPlay = "true"> <válvula className = "org.apache.catalina.valves.accesslogvalve" diretório = "logs" prefix = "localizost_access_log". sufix = ". txt" padrão = " %h %l %u %t" %r " %s %b"/> <!-add (salvar imagens)-> <contexto path = "/upload" docbase = "e:/java/img/upload"> </context> </dost>
3. Caminho real: e:/java/img/upload
4. Caminho de acesso: http: // localhost: 8080/upload/nome de imagem
5. Referência: http://my.oschina.net/pingdy/blog/381001
6. Pré -requisito: o servidor Tomcat deve ser aberto
Exemplo: Exemplo de upload de fotos: (Várias imagens podem ser carregadas)
JsonObject rs = new JsonObject (); Commonsmultipartresolver multipartResolver = new CommonsmultipartResolver (request.getSession (). GetServletContext ()); solicitação; iterator <string> iter = multirequest.getFileNames (); while (iter.hasNext ()) {arquivo multipartFile = multirirequest.getfile (((string) iter.next ()); if (file! = null) {string originalfilename = file.getoriginalfilename (); string [] f = originalfilename.split ("//."); = f [f.Length-1]; System.out.println (ext);} System.out.println (allowImgType == null); if (! allowImgType.Contains (ext.touppercase ())) {rs.put ("code", "errplOad_0003"); Basepath = "/web-inf/Resources/upload"; // string filepathname = request.getSession (). getServletContext (). getRealPath (baseepath); string filepathname = constant.img_path+file.separator+"upload"; url = filepathname; system.out.printlnnnnnnnnnnnnnnnnnnnnnnnn (upload); Arquivo localfile = novo arquivo (filepathname); if (! localfile.exists ()) {localfile.mkdir (); } // compactString fname = new date (). gettime () + "." + ext; string originalfname = fname.substring (0, fname.indexof (".") + "_ original. Arquivo (nome do arquivo); arquivo deile = novo arquivo (OFILENAME); try {imageHelper.comPress (file.getInputStream (), 600, infile); file.Transferto (ofile); // Original Carregue a imagem original jsonObject obj = new jsonObject (); rs.put ("code", constant.code_success); rs. ("dados", "obj.oss", constant.code_success); "Err_upload_0001"); rs.put ("msg", "err_upload_0001"); e.printstacktrace (); retorna rs;}}O exposto acima está relacionado ao upload de fotos e acesso à SpringMVC, e espero que seja útil para todos.