一、使用工具:
nginx-1.8.1.tar.gz
nginx_upload_module-2.2.0.tar.gz
openssl-1.0.2h.tar.gz
pcre-8.37.tar.gz
zlib-1.2.8.tar.gz
GraphicsMagick-1.3.22.tar.gz
apache-tomcat-7.0.47.tar.gz
由于使用的是java语音,基本的配置就不说了。
二、大致记录一下安装过程:
1、解压.tar.gz文件
tar -zxvf pcre-8.37.tar.gz
2、配置
./configure
3、编译
make
4、安装
make install
在安装过程中需要注意的事项:
1、安装的版本需要特别注意。我曾经安装的openssl-fips-2.0.12.tar.gz,nginx_upload_module-2.0.11.tar.gz这两个版本,出现各种无解的问题。
2、nginx的安装配置:
./configure --with-pcre=../pcre-8.37--with-zlib=../zlib-1.2.8 --with-openssl=/home/openssl-1.0.2h --with-http_ssl_module --add-module=/home/nginx_upload_module-2.2.0
2、安装nginx_upload_module-2.2.0.tar.gz时需要下载补丁davromaniak.txt
cd/home/nginx_upload_module-2.2.0
patchngx_http_upload_module.c davromaniak.txt
3、GraphicsMagick在安装过程中需要支持jpg,png格式:
yuminstall -y libpng-devel libpng
yuminstall -y libjpeg-devel libjpeg
三、nginx的上传配置文件
<span style="font-size:18px;">server
{
listen 9001;
server_name 127.0.0.1;
client_max_body_size 50m;
proxy_request_buffering off;
location /upload {
#上传完成后端接受处理文件
upload_pass /img_server/upload;
upload_store /home/upload;#上传文件夹
upload_cleanup 400 404 499 500-505;
upload_limit_rate 0;# 限制上传速度
upload_store_access user:rw;
upload_set_form_field $upload_field_name.name "$upload_file_name";
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";
upload_aggregate_form_field "${upload_field_name}_md5" $upload_file_md5;#md5
upload_aggregate_form_field "${upload_field_name}_size" $upload_file_size;#文件大小
upload_pass_form_field "^submit$|^description$";
#如果希望把所有的表单字段都传给后端可以用
upload_pass_form_field "^.*$";
upload_pass_args on;
}
location /{
proxy_pass http://127.0.0.1:8080;
#proxy_intercept_errors on;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log /usr/local/nginx/logs/img.log;
}</span>
注:upload_pass:为后台处理的路径,需要通过代理服务器转到tomcat服务器中。
四、Java后台得到上传文件的数据方法:
<span style="font-size:14px;"> try {
sis = request.getInputStream();
int a = 0;
int k = 0;
String s = "";
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a);
if ((k = s.indexOf("name=\"")) != -1) {
String fieldName =s.substring(k + 6, s.length() - 3);
sis.readLine(b, 0, b.length);
StringBufferfieldValue = new StringBuffer(b.length);
while ((a = sis.readLine(b, 0, b.length)) != -1) {
s = new String(b, 0, a - 2);
if ((b[0] == 45)&& (b[1] == 45)&& (b[2] == 45)&& (b[3] == 45)&& (b[4] == 45)){
break;
} else {
fieldValue.append(s);
}
}
System.out.println(fieldName+ "---->" + fieldValue.toString());
args.put(fieldName,fieldValue.toString());
}
} catch (Exception e) {
e.getStackTrace();
}</span>
注:上传必须是 POST 方式( enctype="multipart/form-data" )
五、GraphicsMagick对缓存文件做压缩处理
<span style="font-size:14px;">public static String saveMinPhoto(StringsrcPath, String toPath, int width, int height) throws Exception {
System.out.println("----------------saveMinPhoto--------------");
Operation op = new Operation();
ConvertCmd cmd = new ConvertCmd(true);
try {
op.addImage(srcPath);
op.addRawArgs("-sample", width + "x" + height + ">");
op.addRawArgs("-gravity", "center");
op.addRawArgs("-quality", "100");
op.addImage(toPath);
cmd.run(op);
} catch (Exception e) {
System.out.println("报错:" + e.getMessage());
e.printStackTrace();
File file = new File(srcPath);
if (file.exists()) {
file.delete();
}
return "上传失败";
}
return "上传成功";
}</span>
没有将全过程都写下了,只是记录主要的部分和我遇到的问题及其解决方法,还有部分一时想不起来。