Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f891be0

Browse files
Subir archivos
Subir archivos de imagen y almacenarlas en una base de datos, se muestra como visualizar la imagen desde una página JSP.
1 parent 9e62d82 commit f891be0

File tree

10 files changed

+303
-0
lines changed

10 files changed

+303
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-shared-configuration>
3+
<!--
4+
This file contains additional configuration written by modules in the NetBeans IDE.
5+
The configuration is intended to be shared among all the users of project and
6+
therefore it is assumed to be part of version control checkout.
7+
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
8+
-->
9+
<spring-data xmlns="http://www.netbeans.org/ns/spring-data/1">
10+
<config-files>
11+
<config-file>src/main/webapp/WEB-INF/springmvc-servlet.xml</config-file>
12+
</config-files>
13+
<config-file-groups/>
14+
</spring-data>
15+
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
16+
<!--
17+
Properties that influence various parts of the IDE, especially code formatting and the like.
18+
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
19+
That way multiple projects can share the same settings (useful for formatting rules for example).
20+
Any value defined here will override the pom.xml file value but is only applicable to the current project.
21+
-->
22+
<org-netbeans-modules-javascript2-requirejs.enabled>true</org-netbeans-modules-javascript2-requirejs.enabled>
23+
</properties>
24+
</project-shared-configuration>

‎tutorial_spring_upload/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>carmelo.spring</groupId>
5+
<artifactId>tutorial_spring_upload</artifactId>
6+
<packaging>war</packaging>
7+
<version>1.0</version>
8+
<name>tutorial_spring_upload</name>
9+
10+
<properties>
11+
<spring.version>4.3.7.RELEASE</spring.version>
12+
<failOnMissingWebXml>false</failOnMissingWebXml>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
<maven.compiler.source>1.8</maven.compiler.source>
15+
<maven.compiler.target>1.8</maven.compiler.target>
16+
</properties>
17+
18+
<dependencies>
19+
20+
<dependency>
21+
<groupId>org.springframework</groupId>
22+
<artifactId>spring-webmvc</artifactId>
23+
<version>${spring.version}</version>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>org.springframework</groupId>
28+
<artifactId>spring-jdbc</artifactId>
29+
<version>${spring.version}</version>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>org.hsqldb</groupId>
34+
<artifactId>hsqldb</artifactId>
35+
<version>2.3.3</version>
36+
</dependency>
37+
38+
<dependency>
39+
<groupId>javax.servlet</groupId>
40+
<artifactId>javax.servlet-api</artifactId>
41+
<version>3.1.0</version>
42+
<scope>provided</scope>
43+
</dependency>
44+
45+
<dependency>
46+
<groupId>javax.servlet</groupId>
47+
<artifactId>jstl</artifactId>
48+
<version>1.2</version>
49+
</dependency>
50+
51+
<!-- Uplaod Files -->
52+
<dependency>
53+
<groupId>commons-fileupload</groupId>
54+
<artifactId>commons-fileupload</artifactId>
55+
<version>1.3.1</version>
56+
</dependency>
57+
58+
</dependencies>
59+
60+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package carmelo.spring.config;
2+
3+
import java.io.IOException;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.Configuration;
7+
import org.springframework.web.multipart.MultipartResolver;
8+
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
9+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
10+
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
11+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
12+
13+
@EnableWebMvc
14+
@Configuration
15+
@ComponentScan("carmelo.spring.controller")
16+
public class WebAppConfig extends WebMvcConfigurerAdapter {
17+
18+
@Bean
19+
public MultipartResolver multipartResolver() throws IOException {
20+
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
21+
multipartResolver.setMaxUploadSize((1024 * 1024) * 10);
22+
return multipartResolver;
23+
}
24+
25+
@Override
26+
public void configureViewResolvers(ViewResolverRegistry registry) {
27+
registry.jsp("/WEB-INF/views/", ".jsp");
28+
}
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package carmelo.spring.config;
2+
3+
import javax.sql.DataSource;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
8+
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
9+
10+
@Configuration
11+
public class WebAppData {
12+
13+
@Bean
14+
public DataSource dataSource() {
15+
return new EmbeddedDatabaseBuilder()
16+
.setType(EmbeddedDatabaseType.HSQL)
17+
.addScript("classpath:schema.sql")
18+
.build();
19+
}
20+
21+
@Bean
22+
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
23+
return new JdbcTemplate(dataSource);
24+
}
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package carmelo.spring.config;
2+
3+
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
4+
5+
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
6+
7+
@Override
8+
protected Class<?>[] getRootConfigClasses() {
9+
return new Class[]{ WebAppData.class };
10+
}
11+
12+
@Override
13+
protected Class<?>[] getServletConfigClasses() {
14+
return new Class[]{ WebAppConfig.class };
15+
}
16+
17+
@Override
18+
protected String[] getServletMappings() {
19+
return new String[]{"/"};
20+
}
21+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package carmelo.spring.controller;
2+
3+
import java.io.IOException;
4+
import java.util.List;
5+
import java.util.Map;
6+
import javax.servlet.http.HttpServletResponse;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.jdbc.core.JdbcTemplate;
9+
import org.springframework.stereotype.Controller;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestMethod;
12+
import org.springframework.web.bind.annotation.RequestParam;
13+
import org.springframework.web.multipart.MultipartFile;
14+
import org.springframework.web.servlet.ModelAndView;
15+
16+
@Controller
17+
public class ImageController {
18+
19+
@Autowired
20+
private JdbcTemplate jdbc;
21+
22+
@RequestMapping("/")
23+
public ModelAndView home() {
24+
25+
String sql = "SELECT * FROM imagen";
26+
List<Map<String, Object>> imageList = jdbc.queryForList(sql);
27+
28+
return new ModelAndView("home", "imageList", imageList);
29+
}
30+
31+
@RequestMapping("/image")
32+
public ModelAndView image(@RequestParam("nombre") String nombre) {
33+
return new ModelAndView("image", "nombre", nombre);
34+
}
35+
36+
@RequestMapping(path = "/form", method = RequestMethod.POST)
37+
public String handleFormUpload(
38+
@RequestParam("file") MultipartFile file) throws IOException {
39+
40+
if (!file.isEmpty()) {
41+
42+
String sql = "INSERT INTO imagen (nombre, tipo, tamano, pixel) VALUES(?, ?, ?, ?)";
43+
44+
String nombre = file.getOriginalFilename();
45+
String tipo = file.getContentType();
46+
Long tamano = file.getSize();
47+
byte[] pixel = file.getBytes();
48+
49+
jdbc.update(sql, nombre, tipo, tamano, pixel);
50+
}
51+
52+
return "redirect:/";
53+
}
54+
55+
@RequestMapping(value = "/uploaded")
56+
public void getUploadedPicture(
57+
@RequestParam("nombre") String nombre, HttpServletResponse response)
58+
throws IOException {
59+
60+
String sql = "SELECT pixel, tipo FROM imagen WHERE nombre = '" + nombre + "'";
61+
List<Map<String, Object>> result = jdbc.queryForList(sql);
62+
63+
if (!result.isEmpty()) {
64+
byte[] bytes = (byte[]) result.get(0).get("PIXEL");
65+
String mime = (String) result.get(0).get("TIPO");
66+
67+
response.setHeader("Content-Type", mime);
68+
response.getOutputStream().write(bytes);
69+
}
70+
}
71+
72+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE TABLE imagen(nombre VARCHAR(100), tipo VARCHAR(10), tamano BIGINT, pixel BLOB(10M))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Context antiJARLocking="true" path="/upload"/>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<%--
2+
Document : home
3+
Created on : 07/18/2017, 07:55:34 AM
4+
Author : Tutor de programacion
5+
--%>
6+
7+
<%@ page contentType="text/html" pageEncoding="UTF-8"%>
8+
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
9+
10+
<!DOCTYPE html>
11+
<html>
12+
<head>
13+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
14+
<title>Upload File</title>
15+
</head>
16+
<body>
17+
18+
<h1>Spring MVC File Upload</h1>
19+
20+
<form method="post" action="/upload/form" enctype="multipart/form-data">
21+
<input type="file" name="file" accept="image/jpeg,image/png,image/gif"/>
22+
<input type="submit" value="Subir archivo"/>
23+
</form>
24+
25+
<div style="margin-top: 30px;">
26+
27+
<h3>Imágenes en la base de datos</h3>
28+
29+
<table cellspacing="0" cellpadding="6" border="1">
30+
<tr bgcolor="grey" style="color: white">
31+
<th>No</th>
32+
<th>Nombre</th>
33+
<th>Tipo</th>
34+
<th>Tamaño (Bytes)</th>
35+
<th>Ver imagen</th>
36+
</tr>
37+
<c:forEach var="img" items="${imageList}" varStatus="status">
38+
<tr>
39+
<td><b>${status.index + 1}</b></td>
40+
<td>${img.get("NOMBRE")} </td>
41+
<td>${img.get("TIPO")} </td>
42+
<td>${img.get("TAMANO")} </td>
43+
<td><a href="/upload/image?nombre=${img.get("NOMBRE")}">ver</a> </td>
44+
</tr>
45+
</c:forEach>
46+
</table>
47+
</div>
48+
49+
</body>
50+
</html>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<%--
2+
Document : image.jsp
3+
Created on : 07/19/2017, 11:02:20 AM
4+
Author : Tutor de programación
5+
--%>
6+
7+
<%@page contentType="text/html" pageEncoding="UTF-8"%>
8+
<!DOCTYPE html>
9+
<html>
10+
<head>
11+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
12+
<title>Image View</title>
13+
</head>
14+
<body>
15+
<h1>Visor de Imagen</h1>
16+
<h4>Mostrando la imagen: <i>${nombre}</i></h4>
17+
<img src="/upload/uploaded?nombre=${nombre}" />
18+
</body>
19+
</html>

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /