spring mvc+localResizeIMG implements H5 image compression and uploading
This time I will bring you spring mvc+localResizeIMG to realize H5-side image compression and upload. Spring mvc+localResizeIMG realizes H5-side image compression and upload. What are the precautions? What are the practical cases? Let’s take a look. one time.
Recently I was working on a mobile HTML5 application and used the upload function. At first, I used the traditional upload method to upload photos taken by mobile phones. Since the photos taken by mobile phones are usually several MB, the upload speed is very fast. slow.
I have been searching online for a long time and found the localResizeIMG compression framework. I feel it is very practical, so I would like to share it with you here.
Step one: Download localResizeIMG
localResizeIMG is placed in github, the address is: https:// github.com/think2011/localResizeIMG.
Second step: Import localResizeIMG related js in the web project
Decompress localResizeIMG compression and copy the dist folder in the directory to In the project, mine is placed in the js directory.
Then import the js of jQuery and localResizeIMG in your own js. For example:
<span> </span><script></script>"> <span> </span><script></script>">
Step 3: Add the onchange event to the file box of your uploaded input as follows
<input>
In the fileChange method Implement code compression and asynchronously transmit the base64 generated after compression to the background
function fileChange(that){
var filepath=$(that).val();
if(filepath=="")
{
return;
}
var extStart=filepath.lastIndexOf(".");
var ext=filepath.substring(extStart,filepath.length).toUpperCase();
if(".jpg|.png|.bmp|.jpeg".toUpperCase().indexOf(ext.toUpperCase())==-1){
alert("只允许上传jpg、png、bmp、jpeg格式的图片");
return false;
}
//以图片宽度为800进行压缩
lrz(that.files[0], {
width: 800
})
.then(function (rst) {
//压缩后异步上传
$.ajax({
url : "/common/fileUploadPicture",
type: "POST",
data : {
imgdata:rst.base64//压缩后的base值
},
dataType:"json",
cache:false,
async:false,
success : function(data) {
if(data.success)
{
alert(data.message);///data.message为上传成功后的文件路径
}else{
alert(data.message);///data.message为上传失败原因
}
},
error : function(){
alert("上传失败");
}
});
});
}
Step 4: The spring mvc controller background receives the base value and parses and saves the file
import sun.misc.BASE64Decoder;//导入的base64的类
/**
* 文件上传
*/
@ResponseBody
@RequestMapping("common/fileUploadPicture")
public Object fileUploadPicture(String imgdata, HttpServletRequest request) {
LOGGER.info("[文件上传(fileUploadPicture)][params:imgdata=" + imgdata + "]");
BASE64Decoder decoder = new BASE64Decoder();
try {
String basePath = request.getRealPath("/upload_files");
string imgPath=basePath+"/test.jpg";
// new一个文件对象用来保存图片,默认保存当前工程根目录
File imageFile = new File(imgPath);
// 创建输出流
FileOutputStream outputStream = new FileOutputStream(imageFile);
// 获得一个图片文件流,我这里是从flex中传过来的
byte[] result = decoder.decodeBuffer(imgdata.split(",")[1]);//解码
for (int i = 0; i <p style="text-align: left;">Result class: </p><pre class="brush:php;toolbar:false">import java.io.Serializable;
public class Result implements Serializable{
private static final long serialVersionUID = 1L;
private boolean success;
private String message;
public Result() {
success = true;
}
public Result(boolean success, String message) {
this.success = success;
this.message = message;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@Override
public String toString() {
return "Result [success=" + success + ", message=" + message + "]";
}
}I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the use of paging query
How to develop WeChat wall with Node.js
The above is the detailed content of spring mvc+localResizeIMG implements H5 image compression and uploading. For more information, please follow other related articles on the PHP Chinese website!
H5: The Evolution of Web Standards and TechnologiesApr 15, 2025 am 12:12 AMWeb standards and technologies have evolved from HTML4, CSS2 and simple JavaScript to date and have undergone significant developments. 1) HTML5 introduces APIs such as Canvas and WebStorage, which enhances the complexity and interactivity of web applications. 2) CSS3 adds animation and transition functions to make the page more effective. 3) JavaScript improves development efficiency and code readability through modern syntax of Node.js and ES6, such as arrow functions and classes. These changes have promoted the development of performance optimization and best practices of web applications.
Is H5 a Shorthand for HTML5? Exploring the DetailsApr 14, 2025 am 12:05 AMH5 is not just the abbreviation of HTML5, it represents a wider modern web development technology ecosystem: 1. H5 includes HTML5, CSS3, JavaScript and related APIs and technologies; 2. It provides a richer, interactive and smooth user experience, and can run seamlessly on multiple devices; 3. Using the H5 technology stack, you can create responsive web pages and complex interactive functions.
H5 and HTML5: Commonly Used Terms in Web DevelopmentApr 13, 2025 am 12:01 AMH5 and HTML5 refer to the same thing, namely HTML5. HTML5 is the fifth version of HTML, bringing new features such as semantic tags, multimedia support, canvas and graphics, offline storage and local storage, improving the expressiveness and interactivity of web pages.
What Does H5 Refer To? Exploring the ContextApr 12, 2025 am 12:03 AMH5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo
H5: Tools, Frameworks, and Best PracticesApr 11, 2025 am 12:11 AMThe tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.
The Legacy of HTML5: Understanding H5 in the PresentApr 10, 2025 am 09:28 AMHTML5hassignificantlytransformedwebdevelopmentbyintroducingsemanticelements,enhancingmultimediasupport,andimprovingperformance.1)ItmadewebsitesmoreaccessibleandSEO-friendlywithsemanticelementslike,,and.2)HTML5introducednativeandtags,eliminatingthenee
H5 Code: Accessibility and Semantic HTMLApr 09, 2025 am 12:05 AMH5 improves web page accessibility and SEO effects through semantic elements and ARIA attributes. 1. Use, etc. to organize the content structure and improve SEO. 2. ARIA attributes such as aria-label enhance accessibility, and assistive technology users can use web pages smoothly.
Is h5 same as HTML5?Apr 08, 2025 am 12:16 AM"h5" and "HTML5" are the same in most cases, but they may have different meanings in certain specific scenarios. 1. "HTML5" is a W3C-defined standard that contains new tags and APIs. 2. "h5" is usually the abbreviation of HTML5, but in mobile development, it may refer to a framework based on HTML5. Understanding these differences helps to use these terms accurately in your project.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools






