jruby1.0出来了,经理就要求把以前做的一个项目部署在jruby上,转换过去后,什么都好好的,唯一有点问题的就是上传,如果是上传text文件,没有什么问题,但像rar,doc,jpg文件都不行,会报一个:#<NativeException: java.lang.StringIndexOutOfBoundsException: String index out of range: -848>,而且出错的地方是 点submit后 到 doupload 这个动作之前发生的
开始的时候,我认为是url把上传的内容加在url后进行上传的,但上传一个200k的text文件成功,而一个10k的doc不成功,所以不成立,另外一个想法就是,可能是编码的问题,可在text中写入中文,但没有什么影响,实在想不明白是什么回事,也搜索不到什么有效的信息,希望大家指点一下,下面附上部分代码
appication.rb
#编码设置
before_filter :configure_charsets
after_filter :after
def configure_charsets
response.headers["Content-Type"] = "text/html; charset=utf-8"
response.headers["Cache-Control"] = "no-cache"
response.headers["Pragma"] = "no-cache"
suppress(ActiveRecord::StatementInvalid) do
ActiveRecord::Base.connection.execute 'SET NAMES utf8'
end
end
上传操作的action
class UploadController < ApplicationController
IFRAMECTX = <<-html
<style type="text/css">
body {
background-color:#D3DDE7;
margin: 0px;
}
</style>
<body>INFO</body>
html
IMAGE_TYPES = [ "image/jpg", "image/jpeg", "image/pjpeg", "image/gif","image/png" ]
def init
render_text IFRAMECTX.sub('INFO','')
end
def doupload
begin
client_file = params[:file]
if 'image' == params[:mime]
ftype = client_file.content_type.strip
if not IMAGE_TYPES.include?(ftype)
render_text IFRAMECTX.sub('INFO','不支持该格式,上传失败(只支持jpg|gif|png格式的图片)')
return
end
end
if client_file.size > FILEUPLOAD_SIZE
render_text IFRAMECTX.sub('INFO','上传文件过大,限定值为5M')
return
end
server_file = RAILS_ROOT + FILEUPLOAD_TEMPDIR + session.session_id + File.extname(client_file.original_filename)
upload(client_file,server_file)
render_text IFRAMECTX.sub('INFO','上传成功')
rescue => e
@errorNumber = 110 if @errorNumber.nil?
render_text IFRAMECTX.sub('INFO','上传失败')
end
end
end
上传的rhtml
<iframe id="upload_result" name="upload_result" frameborder="0" scrolling="no" height="35" width="600"></iframe></p>
<%= start_form_tag( {:controller=>'/upload', :action=>'doupload'}, :multipart=>true, :target=>'upload_result' ) %>
<label for="file"><%= label%></label><br/><%= file_field_tag "file" %><% if defined? mime %><%= hidden_field_tag('mime',value=mime) %><% end %><%= submit_tag "上传" %>
<%= end_form_tag %>
<script>
$('upload_result').src='/upload/init'
</script>